Crash when adding persistent store (iCloud enabled) in app delegate

前端 未结 5 589
南笙
南笙 2020-12-22 18:00

I am going to start updating this to help those seeking to use this as reference for their own personal code.

Newest update

  • I\'m f
5条回答
  •  不知归路
    2020-12-22 18:23

    UPDATE:

    Everyone should really take a look at the iCloud Core Data session 227 from WWDC 2012. The source code they provide is an excellent starting point for an iCloud based solution. Really take the time to go through what they are doing. You will need to fill in some holes like copying objects from one store to another and de-duping. That being said, I am no longer using the migratePersistentStore approach as described below for moving between local and iCloud stores.


    My original answer:

    Slev asked me to post some code for migrating a store from a local copy to iCloud and back again. This code is experimental and should not be used in production. It is only provided here as reference for us to share and move forward. Once Apple releases a proper reference application, you should probably consult that for your patterns and practices.

    -(void) onChangeiCloudSync
    {
        YourAppDelegate* appDelegate = (YourAppDelegate*) [[UIApplication sharedApplication] delegate];
        NSFileManager *fileManager = [NSFileManager defaultManager];
    
        if ([iCloudUtility iCloudEnabled])
        {
            NSURL *storeUrl = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"YourApp2.sqlite"];
            NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
            NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"];
            cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
    
            //  The API to turn on Core Data iCloud support here.
            NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:@"com.yourcompany.yourapp.coredata", NSPersistentStoreUbiquitousContentNameKey, cloudURL, NSPersistentStoreUbiquitousContentURLKey, [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil];
    
            NSPersistentStore* store = [appDelegate.persistentStoreCoordinator.persistentStores objectAtIndex:0];
            NSError* error;
            if (![appDelegate.persistentStoreCoordinator migratePersistentStore:store toURL:storeUrl options:options withType:NSSQLiteStoreType error:&error])
            {
                NSLog(@"Error migrating data: %@, %@", error, [error userInfo]);
                //abort();
            }
            [fileManager removeItemAtURL:[[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"YourApp.sqlite"] error:nil];
            [appDelegate resetStore];
        }
        else
        {
            NSURL *storeUrl = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"YourApp.sqlite"];
    
            //  The API to turn on Core Data iCloud support here.
            NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                     [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                     nil];
    
            NSPersistentStore* store = [appDelegate.persistentStoreCoordinator.persistentStores objectAtIndex:0];
            NSError* error;
            if (![appDelegate.persistentStoreCoordinator migratePersistentStore:store toURL:storeUrl options:options withType:NSSQLiteStoreType error:&error])
            {
                NSLog(@"Error migrating data: %@, %@", error, [error userInfo]);
                //abort();
            }
            [fileManager removeItemAtURL:[[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"YourApp2.sqlite"] error:nil];
            [appDelegate resetStore];
        }
    }
    

提交回复
热议问题