사용자가 요청한 경우 기존 로컬 스토어를 유비쿼터스 스토어로 마이그레이션해야하는 내 앱으로 iCloud를 사용하려고합니다.기존 코어 데이터 스토어와 iCloud 동기화하기
애플의 dev 포럼과 다른 곳을 둘러 본 후, 나는이 접근 방식을 채택했다. 일관되게 작동하지 않는다. 실제로 작동하는 것을 보았지만 장치 B (iCloud에서 채워짐)에서 여러 번 충돌 한 후에 만 발생합니다.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil)
return persistentStoreCoordinator;
NSURL *legacyStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:NO]]];
NSURL *upgradedStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:YES]]];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if ((IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) && (self.iCloudEnabled)) {
NSPersistentStoreCoordinator* psc = persistentStoreCoordinator;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *cloudOptions = nil;
NSDictionary *localOptions = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"<CONTAINER ID>"];
NSString *coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:[NSString stringWithFormat:@"logs%d",[self activeStoreIndex]]];
if ([coreDataCloudContent length] != 0) {
// iCloud is available
cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
cloudOptions = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
@"MyAppStore", NSPersistentStoreUbiquitousContentNameKey,
cloudURL, NSPersistentStoreUbiquitousContentURLKey,
nil];
} else {
// iCloud is not available
}
NSError *error = nil;
[psc lock];
if(migrateStore) {
migrateStore = NO;
NSPersistentStore *srcPS = [psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:legacyStoreUrl
options:localOptions
error:&error];
if (![psc migratePersistentStore:srcPS
toURL:upgradedStoreUrl
options:cloudOptions
withType:NSSQLiteStoreType
error:&error]) {
NSLog(@"Error migrating data: %@, %@/%@/%@", error, [error userInfo], legacyStoreUrl, upgradedStoreUrl);
abort();
}
}
else {
if (![psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:upgradedStoreUrl
options:(cloudOptions ? cloudOptions : localOptions)
error:&error]) {
NSLog(@"Unresolved iCloud error %@, %@", error, [error userInfo]);
abort();
}
}
[psc unlock];
[[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:legacyStoreUrl options:options error:&error]) {
// error
abort();
}
}
return persistentStoreCoordinator;
}
앱에는 거의 항상 상점이 하나뿐입니다. 2 store (activeStoreIndex 등)의 경우를 처리하기위한 코드는 엣지 경우입니다. – ed94133
migrateStore가 정의되고 할당되는 위치는 어디입니까? – Jim