0

I am developing iOS app with iCloud sync. I stored datas using core data with support of iCloud sync then if i switch off iCloud drive in device settings, My app could not read stored data in UbiquityContainer. Now i want to migrate iCloud persistence store data to local/other place when user iCloud drive is off. But i could not get iCloud persistence store. I tried following methods not working.

NSPersistentStore *result = [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                               configuration:nil
                                         URL:storeURL
                                     options:options
                                       error:nil];

Result is return nil. Because i could not get exact storeURL of ubiquity store.

 NSURL *iCloudURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:@"iCloud.com.Identifier"];

Gives nil instead of url.Is there a way to get iCloud store after switch off iCloud drive. Because i could not display stored in datas.

Madhubalan K
  • 357
  • 5
  • 21
  • I am trying to maintain local (document/local.db) and iCloud (document/ubiqutycontainter/..../iCloud.db) data bases both will maintain same data. If iCloud off means don't need to migrate. Is that correct way?. And how can i sync my local and iCloud db? – Madhubalan K Aug 12 '15 at 06:37
  • Once you have logged out of iCloud you can't access the ubiquity container which is why you need to have an App settings bundle where the user can turn iCloud off for the app before they log out of iCloud. Your app then moves the iCloud store to a local store. See the sample apps here http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/ – Duncan Groenewald Aug 12 '15 at 11:34
  • Hi @DuncanGroenewald i am having app settings to migrate iCloud db to local and vice versa. My problem is when user does not switch off iCloud in app settings and logout from iCloud account means, i am help less. App could not show any data user entered. – Madhubalan K Aug 12 '15 at 14:07
  • That is correct, your app needs to detect when iCloud is turned off but the Apps own iCloud setting is ON and then tell the user that if they want to access their data they need to log back in to iCloud. I would suggest you test out how Apple's own apps work, e.g. Pages or Numbers. – Duncan Groenewald Aug 12 '15 at 22:50
  • @DuncanGroenewald Thanks for quick reply i will check and let you know. – Madhubalan K Aug 13 '15 at 06:17

1 Answers1

1

See the following extract from Apple documents (https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesignForCoreDataIniCloud.html)

Design the Launch Sequence for Your iCloud Core Data App

When you adopt iCloud, take special care when designing the launch sequence for your app. The following factors come into play and you must account for them:

  • The user might or might not have previously indicated a preference to use iCloud in your app; the local instance of your app might or might not have already established its initial store in a ubiquity container.

    As a first step in your launch sequence, read the local user defaults database using the shared NSUserDefaults object. During operation of your app, use that object to save user choices that you’ll need on next launch.

  • The user might log out of iCloud or switch to another account.

    If a user logs out of iCloud, or switches to another account, the ubiquity containers for the previously used account are no longer available to your app.

  • The local Core Data store might be newer or older than the store on
    another device owned by the same user.

    During app launch, Core Data might need to reconcile the local store with change logs from iCloud. This can involve detection and resolution of duplicate records and conflicts. Testing is critical. To get started with some tips, refer to Testing and Debugging Your iCloud App as well as Best Practices and Troubleshooting in iCloud Programming Guide for Core Data.

Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76