मैं iCloud मेरे ऐप है, जो, एक सर्वव्यापी दुकान के लिए एक मौजूदा स्थानीय दुकान विस्थापित करने के लिए करता है, तो उपयोगकर्ता यह अनुरोध करता है की जरूरत है के साथ काम करने के लिए कोशिश कर रहा हूँ के साथ एक मौजूदा कोर डाटा स्टोर।सिंक्रनाइज़ किए जा रहे iCloud
चारों ओर एप्पल देव मंचों कुछ नाउज़िंग और बाद कहीं और, मैं इस दृष्टिकोण है, जो लगातार काम नहीं कर रहा है ले लिया है। मैंने वास्तव में इसे काम देखा है, लेकिन डिवाइस बी पर कई क्रैश के बाद (जो 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 स्टोर्स (ActiveStoreIndex, आदि) के मामले को संभालने के लिए कोड एक बढ़त मामला है। – ed94133
माइग्रेटस्टोर कहां परिभाषित किया गया है और असाइन किया जा रहा है? – Jim