iOS 7核心数据和iCloud同步

时间:2014-07-10 00:58:53

标签: core-data ios7 synchronization icloud

我希望在我的新应用程序中集成在iCloud中同步核心数据的选项,以便在用户设备上共享信息。我环顾网络,但没有找到关于如何使用iOS7做这个的好例子或教程。

我做的最后一件事是分析Apple收据演示应用程序并包含在我的应用程序中。它接缝起作用,至少在第一次观看时。在一台设备上添加记录,一段时间后,另一台设备显示数据 - 到目前为止,我很高兴。

但是,在恢复应用程序后,两个设备上的信息都消失了。所以我查看了应用程序(iExplorer)并找到了本地Core Data,我的所有数据都在那里。我观察到的下一个是调试器显示:(XXX)当然不是真正的值: - )

2014-07-09 19:40:12.830 XXX[199:3507] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](771): CoreData: Ubiquity:  mobile~XXXXX:XXX
Using local storage: 1
2014-07-09 19:40:12.837 XXX[199:60b] asynchronously added persistent store!
2014-07-09 19:40:13.478 XXX[199:1803] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](771): CoreData: Ubiquity:  mobile~XXXXX:XXX
Using local storage: 0

首先它意味着使用本地存储而不是更改为本地存储0。

这是Apple的演示应用程序中使用的代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    // assign the PSC to our app delegate ivar before adding the persistent store in the background
    // this leverages a behavior in Core Data where you can create NSManagedObjectContext and fetch requests
    // even if the PSC has no stores.  Fetch requests return empty arrays until the persistent store is added
    // so it's possible to bring up the UI and then fill in the results later
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];


    // prep the store path and bundle stuff here since NSBundle isn't totally thread safe
    NSPersistentStoreCoordinator* psc = persistentStoreCoordinator;
    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"XXX.sqlite"];

    // do this asynchronously since if this is the first time this particular device is syncing with preexisting
    // iCloud content it may take a long long time to download
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
    // this needs to match the entitlements and provisioning profile
    NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
    NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"XXXXX"];
    cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];

    //  The API to turn on Core Data iCloud support here.
    NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:@"XXX", NSPersistentStoreUbiquitousContentNameKey, cloudURL, NSPersistentStoreUbiquitousContentURLKey, [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil];

    NSError *error = nil;

    [psc lock];
    if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

         Typical reasons for an error here include:
         * The persistent store is not accessible
         * The schema for the persistent store is incompatible with current managed object model
         Check the error message to determine what the actual problem was.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    [psc unlock];

    // tell the UI on the main thread we finally added the store and then
    // post a custom notification to make your views do whatever they need to such as tell their
    // NSFetchedResultsController to -performFetch again now there is a real store
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"asynchronously added persistent store!");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil];
    });
});

return persistentStoreCoordinator;

}

有人可以帮助提供教程或解决方案吗?