2014-07-10 134 views
2

我期待在我的新應用程序中集成iCloud中的核心數據同步功能,以便在用戶設備上共享信息。我環顧網絡,但沒有找到一個很好的例子或教程如何使用iOS7做到這一點。iOS 7核心數據和iCloud同步

我所做的最後一件事就是分析Apple收據演示應用程序並將其包含在我的應用程序中。它接縫工作,至少在第一視圖。在一臺設備上添加一條記錄,一段時間後,另一臺設備顯示數據 - 迄今爲止我很開心。

但是,在恢復應用程序後,信息在兩臺設備上都消失了。所以我查看了應用程序(iExplorer)並找到了本地核心數據,我的所有數據都在那裏。接下來,我觀察到的是調試器顯示這樣的:(XXX)當然不是:-)

2014-07-09 19:40:12.830 XXX[199:3507] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](771): CoreData: Ubiquity: mobile~XXXXX:XXX 
Using local storage: 1 
2014-07-09 19:40:12.837 XXX[199:60b] asynchronously added persistent store! 
2014-07-09 19:40:13.478 XXX[199:1803] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](771): CoreData: Ubiquity: mobile~XXXXX:XXX 
Using local storage: 0 

真正的價值是什麼意思首先它接縫像使用本地存儲,但比變化到本地存儲0

這是來自蘋果的演示應用程序使用的代碼:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    // assign the PSC to our app delegate ivar before adding the persistent store in the background 
    // this leverages a behavior in Core Data where you can create NSManagedObjectContext and fetch requests 
    // even if the PSC has no stores. Fetch requests return empty arrays until the persistent store is added 
    // so it's possible to bring up the UI and then fill in the results later 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 


    // prep the store path and bundle stuff here since NSBundle isn't totally thread safe 
    NSPersistentStoreCoordinator* psc = persistentStoreCoordinator; 
    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"XXX.sqlite"]; 

    // do this asynchronously since if this is the first time this particular device is syncing with preexisting 
    // iCloud content it may take a long long time to download 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 
    // this needs to match the entitlements and provisioning profile 
    NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil]; 
    NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"XXXXX"]; 
    cloudURL = [NSURL fileURLWithPath:coreDataCloudContent]; 

    // The API to turn on Core Data iCloud support here. 
    NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:@"XXX", NSPersistentStoreUbiquitousContentNameKey, cloudURL, NSPersistentStoreUbiquitousContentURLKey, [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil]; 

    NSError *error = nil; 

    [psc lock]; 
    if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
     /* 
     Replace this implementation 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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible 
     * The schema for the persistent store is incompatible with current managed object model 
     Check the error message to determine what the actual problem was. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
    [psc unlock]; 

    // tell the UI on the main thread we finally added the store and then 
    // post a custom notification to make your views do whatever they need to such as tell their 
    // NSFetchedResultsController to -performFetch again now there is a real store 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     NSLog(@"asynchronously added persistent store!"); 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil]; 
    }); 
}); 

return persistentStoreCoordinator; 

}

任何人都可以用的教程或解決方案幫助嗎?

回答

1
+0

感謝您的信息。我看着它,但與iPhoneCoreDataRecipes示例應用程序一樣,所有工作正常,並在設備之間同步。但是,當殺死應用程序或建築物時,信息就消失了。 我正在尋找解決方案來存儲和打開本地CoreData,但通過iCloud同步來自不同設備的信息,併合並新的或更改的本地CoreData內容。 –

+0

示例應用程序始終保留本地存儲,並通過通過iCloud複製更改的事務日誌來同步數據。不確定你的意思是「殺死應用程序或建築物,信息消失」。只有當您從設備上刪除應用程序時,才應刪除本地數據,即使此時您應該能夠在再次安裝應用程序時恢復所有數據。您將需要更多,只是上面的代碼才能正常工作。 –