Search code examples
iosxcodecore-dataicloud

Migrate persistentstore to iCloud


with this code, I can migrate persitentstore (core data) to iCloud:

    NSPersistentStore *persistentStore = [[persistentStoreCoordinator persistentStores]objectAtIndex:0];
    if(![persistentStoreCoordinator migratePersistentStore:persistentStore toURL:[NSURL fileURLWithPath:iCloudData] options:options withType:NSSQLiteStoreType error:&error])
    {
        NSLog(@"errore migrate %@", error);
    }

But I want to migrate only if the Ubiquity Container of iCloud is empty and the persistent store there isn't.


Solution

  • This is identical to the problem described in:

    How to seed initial data to Core Data + iCloud?

    You can't reliably detect the initial state of the container, and therefore have to allow for the possibility of duplicate records after your migration.