I have an app based on cosmicmind graph database published on the AppStore. It saves data locally in the shared sandbox, in order to share the database with the today extension. In the new update I'm trying to add CloudKit feature and it works great, but I have two problems. I'd like that users who already have the app could sync data that are stored locally in the iCloud database, but I don't really know how to do it. The second problem is the today extension. Since the cloud data are stored in a second SQLite file, the extension that was working perfectly before, now returns an empty array. Any help will be really appreciated! Thank you
EDIT Thank you for you fast answer. This is the code I used locally in my datamanager:
func startDataManager() {
let search = Search<Entity>(graph: databaseManager2).for(types: "ElencoItem")
elencoItems = search.sync(completion: nil)
}
and this is the code I use now with icloud:
func startDataManager() {
databaseManager = Graph(cloud: "MyContainer", completion: { (done, error) in
if done {
debugPrint("ok")
let search = Search<Entity>(graph: self.databaseManager).for(types: "ElencoItem")
self.elencoItems = search.sync()
} else {
guard let error = error else { return }
debugPrint("error iCloud: \(error.localizedDescription)")
}
})
I also added all delegate methods and it works perfectly.
I solved the issue with today extension. The problem was that the extension didn't see the array saved for cloud version. I saved like this:
let nuovo = Entity(type: "ElencoItem", graph: DataManager.shared.databaseManager)
nuovo["value"] = value
Now I save cloud data to a new array like:
let search = Search<Entity>(graph: DataManager.shared.databaseManager).for(types: "ElencoItem")
for item in search.sync() { {
let nuovo = Entity(type: "ElencoItem")
nuovo["value"] = item["value"]
DataManager.shared.elenco.append(nuovo)
DataManager.shared.databaseManager2.sync()
}
and the extension works great.
I'm trying now to migrate data saved locally to cloud but it doesn't work. Data were saved locally like this:
let nuovo = Entity(type: "ElencoItem")
nuovo["value"] = value
I do the same as above to move data to cloud save like:
let search = Search<Entity>(graph: self.databaseManager10).for(types: "ElencoItem")
for item in search.sync() {
let nuovo = Entity(type: "ElencoItem", graph: DataManager.shared.databaseManager)
nuovo["value"] = item["value"]
DataManager.shared.elencoItems.append(nuovo)
DataManager.shared.databaseManager.sync()
print(DataManager.shared.elencoItems)
}
When I print the array after the for loop I can see all data, but after closing the app I can't find data anymore. Any solution? Thanks