Closed
Description
hi @JohnEstropia, i have some problem about corestore observer, can you help me?
this my method when fetch data from server and save it into database
try! Omni.dataStack.perform(synchronous: { transaction in
try! Omni.deactivateAll(transaction: transaction,
objects: [Omni.Group.self, Omni.Variant.self, Omni.Product.self,
Omni.Modifier.self, Omni.ModifierGroup.self])
let groups = try! transaction.importUniqueObjects(Into<Omni.Group>(),
sourceArray: json["groups"].arrayValue)
let products = try! transaction.importUniqueObjects(Into<Omni.Product>(),
sourceArray: json["products"].arrayValue)
let productMapping = products.reduce(into: [Int: Omni.Product]()) { (result, object) in
result[object.id] = object
}
let groupMapping = groups.reduce(into: [Int: Omni.Group]()) { (result, object) in
result[object.id] = object
}
_ = try! transaction.importUniqueObjects(
Into<Omni.Variant>(),
/**
json["variants"] represents a list of variant objects obtained from the API.
JSON Structure:
[
{
"id": Int,
"code": String,
"name": String,
....
},
]
Example File: Tests/Resources/JSONFiles/inventories_with_no_modifiers.json
*/
sourceArray: json["variants"].arrayValue, preProcess: { mapping in
var variantMapping = [Int: JSON]()
for (key, variantJSON) in mapping {
guard var dict = variantJSON.dictionaryObject else { continue }
if let productId = variantJSON["product_id"].int {
dict["product_object"] = productMapping[productId]
}
var groupObjects = Set<Omni.Group>()
for groupId in variantJSON["groups"].arrayValue {
if let group = groupMapping[groupId.intValue] {
groupObjects.insert(group)
}
}
dict["group_objects"] = groupObjects
variantMapping[key] = JSON(dict)
}
return variantMapping
}
)
})
and i add new observer in viewcontroller to notify when variant get updated, so i will fetch all groups data, but groups data is not get the latest one
variantPublisher.addObserver(self, notifyInitial: true) { [weak self] publisher in
guard let self else { return }
let groups = try! Omni.dataStack.fetchAll(
From<Omni.Group>()
.where(\.$isActive == true && \.$typeValue == GroupTypes.regular.rawValue)
.orderBy(.ascending(\.$position))
)
self.categories = CategoryViewModel.forDisplay(groups: groups)
}
Metadata
Metadata
Assignees
Labels
No labels