[英]How to enable iCloud for Core Data?
這是我第一次嘗試進行這項工作。 我正在遵循《 iCloud核心數據編程指南 》,“將SQLite存儲與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;
}
以前,我為此應用程序ID啟用了iCloud服務,創建了相應的配置,並以Xcode的功能(iCloud文檔和默認容器)打開了iCloud服務。
根據《 iCloud Programming Guide for Core Data
,此時:
測試:在設備上運行該應用程序。 如果Core Data成功創建並配置了啟用iCloud的持久性存儲,則框架會記錄一條消息,其中包含“使用本地存儲:1”,然后,另一條消息中包含“使用本地存儲:0”。
我在iPhone上運行該應用程序,並且檢索了persistentStoreCoordinator
,但在Xcode的調試區域中看不到任何東西。
我想念什么?
謝謝?
我發現了問題: [[NSFileManager defaultManager] ubiquityIdentityToken]
返回nil
,這是因為我的設備未更新到iCloud Drive
。
我在這篇文章中找到了回應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.