我在現有應用程序中使用核心數據。現在我想集成iCloud,以便用戶可以在他們的iOS設備之間同步他們的內容。要做到這一點,我寫了下面的代碼爲我NSPersistentStoreCoordinator(當然佔位符在我的代碼填寫):iOS:將現有核心數據庫遷移到iCloud中
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"<DB_Name>.sqlite"];
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSPersistentStoreCoordinator* psc = persistentStoreCoordinator_;
if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
// Migrate datamodel
NSDictionary *options = nil;
// this needs to match the entitlements and provisioning profile
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"<App_Identifier>"];
NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"];
if ([coreDataCloudContent length] != 0) {
// iCloud is available
cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
@"<App_Name>.store", NSPersistentStoreUbiquitousContentNameKey,
cloudURL, NSPersistentStoreUbiquitousContentURLKey,
nil];
} else {
// iCloud is not available
options = [NSDictionary dictionaryWithObjectsAndKeys:
[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])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[psc unlock];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"asynchronously added persistent store!");
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil];
});
});
} else {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
NSError *error = nil;
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
return persistentStoreCoordinator_;
}
與該代碼的所有新增數據記錄都iOS-之間自動同步設備,所以它的工作方式應該完全相同!
但我想要的是,所有現有的數據記錄在所有設備之間同步;那還不行。現有的記錄在應用程序中仍然可用,可以使用,但它們不同步。
我需要做些什麼來獲得與iCloud同步的所有現有數據記錄?我已經用方法試驗了一下
migratePersistentStore:toURL:options:withType:error:
但沒有任何成功。
我非常感謝任何幫助!
示例代碼,您需要遷移現有數據的一些特殊代碼。 Google周圍 - 已經有解決方案。您也可以在Apple Developer的論壇上查看此主題 - https://devforums.apple.com/thread/126670?tstart=0您必須擁有有效的開發者ID才能訪問它。首先查看線程的末尾(這很長)。 – 2012-04-27 08:59:48