[英]iCloud Core Data Sync Setting
我正在開發一個將iCloud與其核心數據存儲區進行同步的應用程序(具有單個存儲區/模型/上下文的簡單應用程序)。 由於商店還包含圖像數據,因此有可能變得很大,因此我想添加一個設置,以允許用戶根據需要禁用同步。 我看了一些在兩種情況下都使用Core Data的示例代碼,在我看來,啟用和禁用iCloud時運行的唯一真正區別是添加時將傳遞給NSPersistentStoreCoordinator
的選項。 因此,我不得不這樣做:
NSPersistentStoreCoordinator *psc;
NSDictionary* options;
//Set options based on iCloud setting
if ([enableSwitch isOn]) {
options = [NSDictionary dictionaryWithObjectsAndKeys:
@"<unique name here>", NSPersistentStoreUbiquitousContentNameKey,
cloudURL, NSPersistentStoreUbiquitousContentURLKey,
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
} else {
options = nil;
}
//Add the coordinator
if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
//Handle error
}
因此,我對上述問題有兩個疑問:
謝謝!
這是我想出的解決方案,並且運行良好。 基本上,以下代碼位於一種方法中,您可以在應用程序啟動時或在面向用戶的“啟用iCloud Sync”設置發生更改時調用此方法。 兩種操作模式之間需要更改的全部是NSPersistentStore
實例,基礎模型文件和協調器無需更改。
我的應用程序只需要擔心一個持久性存儲,因此在調用此方法時,它只刪除當前附加到協調器的所有存儲,然后根據用戶設置使用適當的選項創建一個新存儲。
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DataModel.sqlite"];
NSError *error = nil;
NSArray *stores = [__persistentStoreCoordinator persistentStores];
for (int i=0; i < [stores count]; i++) {
[__persistentStoreCoordinator removePersistentStore:[stores objectAtIndex:i] error:&error];
}
//Determine availability. If nil, service is currently unavailable
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
if (!cloudURL) {
NSLog(@"iCloud currently unavailable.");
}
//Check if user setting has been set
if (![[NSUserDefaults standardUserDefaults] objectForKey:kCloudStorageKey]) {
//Set the default based on availability
BOOL defaultValue = (cloudURL == nil) ? NO : YES;
[[NSUserDefaults standardUserDefaults] setBool:defaultValue forKey:kCloudStorageKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//Set options based on availability and use settings
BOOL cloudEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:kCloudStorageKey];
NSDictionary *options = nil;
if (cloudEnabled && cloudURL) {
NSLog(@"Enabling iCloud Sync in Persistent Store");
options = [NSDictionary dictionaryWithObjectsAndKeys:
@"<awesome.unique.name.key>", NSPersistentStoreUbiquitousContentNameKey,
cloudURL, NSPersistentStoreUbiquitousContentURLKey,
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
}
//Add the store with appropriate options
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
如果您有一個利用多個商店的應用程序,則可能需要保留對要啟用/禁用同步的商店的引用,以便您可以采用一種更智能的方法,而不是每次都將其全部銷毀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.