20

I've managed to get my existing core data app to work with iCloud. After days of study, it was actually surprisingly simple. It seems that 3 things are essential:

  • to add an entitlements file (in recent Xcode, this can be done using by selecting the target, select "Summary pane", scroll down, check enable entitlements"

  • to add the correct options while adding the persisten store, in my case

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
        // other options
        @"<arbitrary name>", NSPersistentStoreUbiquitousContentNameKey,
        iCloudURL, NSPersistentStoreUbiquitousContentURLKey,
        nil]
    

    where

    NSURL * iCloudURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    

The `nil' here indicating that information from the Entitlements.plist file is used.

  • enabling iCloud support for the app through the developer portal. This might involve generating a unique app-ID, something I hadn't done before.

Actually, at the moment I am not sure this last step is crucial for development---i've enabled another app without this last step and it seems to work.

Anyhow, I've noticed that two existing core data bases of the same app on different iDevices will synchronise new entries to the core data stack, but will not automatically sync the existing records.

One way of syncing data from device A to B is to delete the existing core data database on B, and then restart the app on B. However, this is not a proper merge.

Does anybody know a way to merge two existing core data databases on different apps at the moment the apps are upgraded to use iCloud support, i.e., use the options above and all that?

Thanks

MPelletier
  • 16,256
  • 15
  • 86
  • 137
davidav
  • 860
  • 8
  • 12
  • maybe can you find help in this similar question http://stackoverflow.com/questions/6588180/can-you-sync-coredata-with-icloud –  Jan 23 '12 at 23:42
  • start here - https://devforums.apple.com/thread/126670 – Kostiantyn Sokolinskyi Jan 30 '12 at 10:18
  • No, although the Recipes app contains useful code (among which adding the store to the persistent store coordinator in the background---useful when merging large core-data databases) it doesn't address the issue of merging two pre-existing core-data instances on two different devices. I was looking for a way of replaying the transaction logs somehow. – davidav Feb 06 '12 at 23:16
  • Any progress @davidav? I'm stuck with the same issue. – damon Feb 14 '12 at 19:43
  • No, not really. I am getting weirder behavior nowadays. It appears that some updates get lost, whicht makes the DBs on the two different devices different. I'm now using the method of starting the persistent store coordinator in a separate thread---like recipes above---but that doesn't make things any better or more transparent. – davidav Feb 17 '12 at 22:26

2 Answers2

2

Maybe this helps: https://gist.github.com/1475162 (by @steipete)

pencil
  • 1,165
  • 8
  • 20
  • 1
    I believe this link is for UIDocument based syncing, and the question is asking about Core Data syncing. – kurtzmarc Aug 10 '12 at 00:07
1

I have not done it, but when saving to the iCloud there is high risk for conflicting information if the data has also been updated or exists on another device. If the conflicts are not properly resolved then the flow between the two devices will not occur properly. Resolving the conflicts can be complicated but the simplest would be to just let the most recent win. Apple has conflict handling procedures that get triggered when saving to overwrite to the ubiquitous store identifies a conflict.

LavaSlider
  • 2,494
  • 18
  • 29
  • The core data iCloud implementation is actually quite good, you typically don't run in too many conflicts. Perhaps if you're editing the same record in two different instances, probably the last saved record will eventually prevail. – davidav Feb 06 '12 at 23:18