-1

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

filop
  • 11
  • 3

2 Answers2

0

So Graph has a sync mechanism with iCloud. If you use that, you would only need to write a migration function to move your data over, and then update your app to tap into the iCloud API. For the Today extension... not sure what that is.

CosmicMind
  • 1,499
  • 1
  • 10
  • 6
  • I edited the question to show how I'm trying to migrate data. Can you tell me what's wrong? Thanks – filop May 20 '17 at 15:01
  • After many tests, moving the migration function in different locations, I get the following error: "fatal error: [Graph Error: Cannot obtain permanent objectID]". I really don't know haw to solve. Any idea @CosmicMind ? Thank you – filop May 22 '17 at 13:14
0

Problem solved. I moved the migration function from the datamanager to the first viewcontroller loaded, removed the print statement and it works perfectly!

filop
  • 11
  • 3