Core data iOS 7核心数据和iCloud同步

Core data iOS 7核心数据和iCloud同步,core-data,ios7,synchronization,icloud,Core Data,Ios7,Synchronization,Icloud,我希望在我的新应用程序中集成在iCloud中同步核心数据的选项,从而在用户设备上共享信息。我在网上四处寻找,但还没有找到一个关于如何使用iOS7实现这一点的好例子或教程 我最后做的是分析苹果收据演示应用程序,并将其包含在我的应用程序中。至少从第一眼看,这是一种工作。在一台设备上添加一条记录,过了一会儿,另一台设备显示了数据——到目前为止,我很高兴 但是,在恢复应用程序后,两台设备上的信息都消失了。因此,我查看了应用程序(iExplorer),找到了本地核心数据,我的所有数据都在那里。我观察到的下

我希望在我的新应用程序中集成在iCloud中同步核心数据的选项,从而在用户设备上共享信息。我在网上四处寻找,但还没有找到一个关于如何使用iOS7实现这一点的好例子或教程

我最后做的是分析苹果收据演示应用程序,并将其包含在我的应用程序中。至少从第一眼看,这是一种工作。在一台设备上添加一条记录,过了一会儿,另一台设备显示了数据——到目前为止,我很高兴

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

什么意思?首先它喜欢使用本地存储,但不想更改为本地存储0

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

- (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;
}


是否有人可以提供教程或解决方案方面的帮助?

尝试这些针对iOS和OSX的示例应用程序


谢谢您提供的信息。我看了一下,但和iPhoneCoreDataRecipes示例应用一样,所有这些都可以正常工作,并且可以在设备之间同步。但当杀死应用程序或建筑时,信息就消失了。我正在寻找一种解决方案来始终存储和打开本地CoreData,但通过iCloud同步来自不同设备的信息,并将新的或更改的内容合并到本地CoreData中。示例应用程序始终保留一个本地存储,并通过iCloud复制更改的事务日志来同步数据。不知道你所说的“杀死应用程序或构建,信息消失”是什么意思。只有从设备中删除应用程序时,才应删除本地数据,即使如此,在再次安装应用程序时,也应能够恢复所有数据。要使其正常工作,您需要的不仅仅是上面的代码。
- (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;