【问题标题】:How to enable iCloud for Core Data?如何为 Core Data 启用 iCloud?
【发布时间】:2015-04-09 14:56:39
【问题描述】:

这是我第一次尝试完成这项工作。我正在关注iCloud Programming Guide for Core Data,“将 SQLite Store 与 iCloud 一起使用”部分,在 AppDelegate 我有:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
   // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
   if (_persistentStoreCoordinator != nil) {
       return _persistentStoreCoordinator;
   }

   // Create the coordinator and store

   _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
   NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite"];
   NSError *error = nil;
   NSString *failureReason = @"There was an error creating or loading the application's saved data.";

   // To enable iCloud
   NSDictionary *storeOptions = @{NSPersistentStoreUbiquitousContentNameKey: @"MyAppCloudStore"};

   if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:storeOptions error:&error]) {
       // Report any error we got.
       NSMutableDictionary *dict = [NSMutableDictionary dictionary];
       dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
       dict[NSLocalizedFailureReasonErrorKey] = failureReason;
       dict[NSUnderlyingErrorKey] = error;
       error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
       // Replace this 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.
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
       abort();
   }

   return _persistentStoreCoordinator;
}

之前,我为这个 App ID 启用了 iCloud 服务,创建了相应的配置,并在 Xcode 的功能(iCloud 文档和默认容器)中打开了 iCloud 服务。

根据iCloud Programming Guide for Core Data,此时:

测试:在设备上运行应用。如果 Core Data 成功创建并配置了支持 iCloud 的持久存储,框架会记录一条包含“使用本地存储:1”的消息,然后再记录一条包含“使用本地存储:0”的消息。

我在 iPhone 上运行应用程序,并检索到 persistentStoreCoordinator,但我在 Xcode 的调试区域中看不到任何内容。

我错过了什么?

谢谢?

【问题讨论】:

    标签: ios sqlite core-data icloud


    【解决方案1】:

    我发现了问题:[[NSFileManager defaultManager] ubiquityIdentityToken] 正在返回 nil,这是因为我的设备没有更新到 iCloud Drive

    我在this post找到了回复。

    【讨论】:

      猜你喜欢
      • 2011-12-07
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多