如何启用iCloud核心数据?
问题描述:
这是我第一次尝试做这项工作。我跟随iCloud Programming Guide for Core Data,“使用的SQLite存储与iCloud的”一节,并在AppDelegate
我:如何启用iCloud核心数据?
- (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;
}
以前,我启用的iCloud服务,为这个应用程序ID,创建相应的配置,并打开Xcode功能中的iCloud服务(iCloud Documents和默认容器)。
按照iCloud Programming Guide for Core Data
,在这一点上:
测试:在设备上运行的应用程序。如果Core Data成功创建并配置了启用iCloud的持久性存储,则框架会记录一条消息,其中包含“使用本地存储:1”以及稍后包含“使用本地存储:0”的另一条消息。
我在iPhone上运行应用程序,并检索到persistentStoreCoordinator
,但我在Xcode的调试区域中什么也看不到。
我错过了什么?
谢谢?
答
我发现问题:[[NSFileManager defaultManager] ubiquityIdentityToken]
返回nil
,这是因为我的设备未更新到iCloud Drive
。
我在this post找到了答复。