![](/img/trans.png)
[英]Will iCloud sync all CoreData data when using CoreData in iCloud
[英]iCloud & CoreData: Notification when changed to iCloud Store (First Launch with existing iCloud Data)
當本地存儲更改為iCloud存儲時,我正在嘗試接收消息。
這是關鍵事件。 所以我的用例是一個新設備,從一個空的存儲開始,然后接收iCloud存儲。 我想通知視圖以接收到的內容進行更新。
我這樣初始化托管對象上下文:
[self.managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:self.storeURL
options:@{ NSPersistentStoreUbiquitousContentNameKey : @"iCloudStore", [NSNumber numberWithBool:YES]: NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES]:NSInferMappingModelAutomaticallyOption}
error:&error];
我的下一步是獲取以下通知:
NSNotificationCenter *dc = [NSNotificationCenter defaultCenter];
[dc addObserver:self
selector:@selector(storesWillChange:)
name:NSPersistentStoreCoordinatorStoresWillChangeNotification
object:psc];
[dc addObserver:self
selector:@selector(storesDidChange:)
name:NSPersistentStoreCoordinatorStoresDidChangeNotification
object:psc];
[dc addObserver:self
selector:@selector(persistentStoreDidImportUbiquitousContentChanges:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:psc];
我認為以name:NSPersistentStoreCoordinatorStoresDidChangeNotification
來實現更新ui應該可以解決。 但是,某種程度上看來,這就是我打算要做的。
編輯:在接受的答案中的相關文章,我可以解決我的問題
我在檢查用戶通知
像這樣: storesDidChange:
NSNumber *storeVal = [note.userInfo objectForKey:NSPersistentStoreUbiquitousTransitionTypeKey];
if (storeVal.integerValue == NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted) {
//you are now in sync with iCloud
NSLog(@"On iCloud Store now");
[self.delegate storeHasChanged];
}
有關處理核心數據存儲更改事件的描述,請參見下面的鏈接。 請注意,無論商店的狀態如何,第一個storeDidChange通知都是相同的。 但是,如果這是您第一次創建商店,並且已經有一個iCloud商店,那么在現有iCloud商店初始導入完成后,您將收到另一個storeDidChange通知。
問題是,除非您知道自己正在創建新商店並且iCloud中已經存在商店,否則您在發生情況之前不知道情況如何。
不幸的是,正如我在解釋中指出的那樣,本地存儲和iCloud存儲之間沒有真正的切換-但是Core Data確實以某種方式導入了sideLoad存儲,這時您會獲得過渡類型4通知(第二個storeDidChange)。
另請注意,如果您的商店需要升級到新的模型版本,那么您還會收到一大堆的storeDidChange通知...
您可能還想在相同的鏈接http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-上查看示例應用程序的工作方式。 核心數據的應用程序與- icloud的集成/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.