Move local Core Data to iCloud

后端 未结 2 1118
别跟我提以往
别跟我提以往 2021-01-30 07:34

How can I enable iCloud Core Data in an app which already uses local storage Core Data?

I\'ve tried to use NSPersistentStoreUbiquitousContentNameKey in my p

2条回答
  •  粉色の甜心
    2021-01-30 08:04

    Here's what you'll need to do

    1. Create a local NSPersistentStoreCoordinator
    2. Add your existing persistent store to that coordinator and store a reference to this new returned store.
    3. Call that handy migratePersistStore:... providing the store from #2, a URL for the store in the documents directory with a different file name and the all important options including the NSPersistentStoreUbiquitousContentNameKey key.

    Here's the code, notes in-line.

    NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    
    //This is the path to the new store. Note it has a different file name
    NSURL *storeURL = [documentsDirectory URLByAppendingPathComponent:@"TestRemote.sqlite"];
    
    //This is the path to the existing store
    NSURL *seedStoreURL = [documentsDirectory URLByAppendingPathComponent:@"Test.sqlite"];
    
    //You should create a new store here instead of using the one you presumably already have access to
    NSPersistentStoreCoordinator *coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
    
    NSError *seedStoreError;
    NSDictionary *seedStoreOptions = @{ NSReadOnlyPersistentStoreOption: @YES };
    NSPersistentStore *seedStore = [coord addPersistentStoreWithType:NSSQLiteStoreType
                                                       configuration:nil
                                                                 URL:seedStoreURL
                                                             options:seedStoreOptions
                                                               error:&seedStoreError];
    
    NSDictionary *iCloudOptions = @{ NSPersistentStoreUbiquitousContentNameKey: @"MyiCloudStore" };
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    //This is using an operation queue because this happens synchronously
    [queue addOperationWithBlock:^{
        NSError *blockError;
        [coord migratePersistentStore:seedStore
                                toURL:storeURL
                              options:iCloudOptions
                             withType:NSSQLiteStoreType
                                error:&blockError];
    
        NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
        [mainQueue addOperationWithBlock:^{
            // This will be called when the migration is done
        }];
    }];
    

    Note that after you do this migration, you'll need to configure the persistent store you use with your MOC with the new URL and always include the iCloudOptions above with the NSPersistentStoreUbiquitousContentNameKey key.

    This was based on Apple's documentation.

    After completion, you should see a new folder in your Documents folder in the simulator folder (~/Library/Application Support/iPhone Simulator/...) labeled CoreDataUbiquitySupport. Nested deep in there is your iCloud synced sqlite store.

    Tada!


    EDIT: Oh and make sure you have created an iCloud entitlement and included it in your bundle. You should be able to do that all within Xcode, but you can update it on the development portal too.

提交回复
热议问题