【问题标题】:iOS 7 Core Data and iCloud synciOS 7 Core Data 和 iCloud 同步
【发布时间】:2014-08-31 04:52:23
【问题描述】:

我希望在我的新应用中集成在 iCloud 中同步核心数据的选项,以便在用户设备上共享信息。我在网上四处查看,但没有找到关于如何使用 iOS7 执行此操作的好示例或教程。

我所做的最后一项工作是分析 Apple 收据演示应用并包含在我的应用中。它似乎可以工作,至少在第一眼看来。在一台设备上添加记录,不久之后,另一台设备会显示数据 - 到目前为止我很高兴。

但是,在恢复应用程序后,两台设备上的信息都消失了。所以我查看了应用程序(iExplorer)并找到了本地核心数据,我的所有数据都在那里。我观察到的下一个是调试器显示:(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;

}

任何人都可以提供教程或解决方案吗?

【问题讨论】:

    标签: core-data ios7 synchronization icloud


    【解决方案1】:

    【讨论】:

    • 感谢您的信息。我看了看,但与 iPhoneCoreDataRecipes 示例应用程序一样,一切正常,并且在设备之间同步。但是当杀死应用程序或构建时,信息就消失了。我正在寻找始终存储和打开本地 CoreData 的解决方案,但通过 iCloud 同步来自不同设备的信息并在本地 CoreData 中合并新的或更改的内容。
    • 示例应用程序始终保留本地存储并通过通过 iCloud 复制更改的事务日志来同步数据。不确定“杀死应用程序或构建,信息消失”是什么意思。仅当您从设备中删除该应用程序时,才应删除本地数据,即使如此,当您再次安装该应用程序时,您也应该能够恢复所有数据。除了上面的代码之外,您还需要更多代码才能使其正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 2013-11-26
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    相关资源
    最近更新 更多