2014-12-27 167 views
2

如何用iCloud核心數據取代我現有的核心數據? 這是我persistentStoreCoordinator:Swift - 用iCloud核心數據取代核心數據

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { 
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail. 
    // Create the coordinator and store 
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) 

    var notificacionCenter: NSNotificationCenter = NSNotificationCenter.defaultCenter() 
    notificacionCenter.addObserver(self, selector: "storesWillChange:", name:NSPersistentStoreCoordinatorStoresWillChangeNotification, object: coordinator) 
    notificacionCenter.addObserver(self, selector: "storesDidChange:", name:NSPersistentStoreCoordinatorStoresDidChangeNotification, object: coordinator) 
    notificacionCenter.addObserver(self, selector: "persistentStoreDidImportUbiquitousContentChanges:", name:NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: coordinator) 

    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyApp.sqlite") 
    var error: NSError? = nil 
    var failureReason = "There was an error creating or loading the application's saved data." 
    var options: NSDictionary = [NSMigratePersistentStoresAutomaticallyOption : NSNumber(bool: true), NSInferMappingModelAutomaticallyOption : NSNumber(bool: true), 
     NSPersistentStoreUbiquitousContentNameKey : "iCloudStore"] 
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options, error: &error) == nil { 
     coordinator = nil 
     // Report any error we got. 
     let dict = NSMutableDictionary() 
     dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" 
     dict[NSLocalizedFailureReasonErrorKey] = failureReason 
     dict[NSUnderlyingErrorKey] = error 
     error = NSError(domain: "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 coordinator 
}() 

我補充通知,然後呢?如何更換它?

回答

5

您不需要做任何事情就可以讓iCloud存儲的數據替換本地存儲的數據。也就是說,使用iCloud下載的數據更新本地核心數據,並在此後保持一致。

但是,如果您已在用戶界面中顯示數據,那麼當「新的iCloud數據」到達時,「舊本地數據」可能仍會顯示。 Using the SQLite Store with iCould的Apple文檔描述瞭如何通過通知來處理更新。總結該引用:在NSPersistentStoreCoordinatorStoresWillChangeNotification的處理程序中,如果存在任何掛起的更改(如果它們存在),則保存它,否則您重置託管對象上下文並禁用用戶界面。然後在NSPersistentStoreCoordinatorStoresDidChangeNotification處理程序中更新顯示的(和其他)數據並重新啓用您的UI。

這是我用於Core Data w/iCloud的。 storeOptions是關鍵。對於您的Xcode目標,您需要啓用iCloud功能,並確保您已選擇「iCloud文檔」。

{ 
    // ... 

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { 
    // The persistent store coordinator for the application. This implementation 
    // creates and return a coordinator, having added the store for the 
    // application to it. This property is optional since there are legitimate 
    // error conditions that could cause the creation of the store to fail. 
    // Create the coordinator and store 
    var coordinator: NSPersistentStoreCoordinator = 
     NSPersistentStoreCoordinator (managedObjectModel: self.managedObjectModel) 

    var storeURL = 
     self.applicationDocumentsDirectory 
     .URLByAppendingPathComponent("iAppSwift.sqlite") 

    var storeOptions = 
     [NSPersistentStoreUbiquitousContentNameKey : "iAppSwiftStore" 
     // NSPersistentStoreRebuildFromUbiquitousContentOption: @(true) 
     ] 

    // 
    self.registerCoordinatorForStoreNotifications (coordinator) 

    var error : NSError? = nil 
    var store : NSPersistentStore! = 
     coordinator.addPersistentStoreWithType (NSSQLiteStoreType, 
     configuration: nil, 
     URL: storeURL, 
     options: storeOptions, 
     error: &error) 

    if nil == store { 
     // handle error 
    } 

    return coordinator 
    }() 

    func registerCoordinatorForStoreNotifications (coordinator : NSPersistentStoreCoordinator) { 
    let nc : NSNotificationCenter = NSNotificationCenter.defaultCenter(); 

    nc.addObserver(self, selector: "handleStoresWillChange:", 
     name: NSPersistentStoreCoordinatorStoresWillChangeNotification, 
     object: coordinator) 

    nc.addObserver(self, selector: "handleStoresDidChange:", 
     name: NSPersistentStoreCoordinatorStoresDidChangeNotification, 
     object: coordinator) 

    nc.addObserver(self, selector: "handleStoresWillRemove:", 
     name: NSPersistentStoreCoordinatorWillRemoveStoreNotification, 
     object: coordinator) 

    nc.addObserver(self, selector: "handleStoreChangedUbiquitousContent:", 
     name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, 
     object: coordinator) 
    } 

    // ... 
} 
+0

現在呢?我的核心數據與iCloud合併?如何在其他設備上檢測是否更改刷新UI? – 2014-12-29 14:09:35

+0

'其他設備'將得到'WillChange'和'DidChange'通知。在處理這些通知的處理程序中,您將更新顯示的(和其他)數據。 – GoZoner 2014-12-30 06:37:11

+0

'var options:NSDictionary = [NSMigratePersistentStoresAutomaticallyOption:NSNumber(bool:true),NSInferMappingModelAutomaticallyOption:NSNumber(bool:true), NSPersistentStoreUbiquitousContentNameKey:「iCloudStore」iCloud已激活? – 2014-12-30 11:07:03