我有以下问题。 我已经成功创建了我的iCloud魔法记录存储,如果从零填充它也很有效:)
但是现在我在旧的Magical Record Local Store中有很多数据,我想将它带到iCloud商店。 但我如何迁移它们。
if ([[NSFileManager defaultManager] ubiquityIdentityToken]) {
[self iCloudCoreDataSetup]}
else {
NSLog(@"iCloud not enabled");
[MagicalRecord setupAutoMigratingCoreDataStack];
}
我在这里检查iCloud是否可用。在iCloud之前,我总是用
进行初始化[MagicalRecord setupAutoMigratingCoreDataStack];
以下是我的iCloud安装代码
- (void)iCloudCoreDataSetup {
// containerID should contain the same string as your iCloud entitlements
NSString *containerID = [NSString stringWithFormat:@"iCloud.%@", [[NSBundle mainBundle] bundleIdentifier]];
[MagicalRecord setupCoreDataStackWithiCloudContainer:containerID
contentNameKey:@"App" // Must not contain dots
localStoreNamed:@"App.sqlite"
cloudStorePathComponent:@"Documents/CloudLogs" // Subpath within your ubiquitous container that will contain db change logs
completion:^{
// This gets executed after all the setup steps are performed
// Uncomment the following lines to verify
NSLog(@"%@", [MagicalRecord currentStack]);
// NSLog(@"%i events", [Event countOfEntities]);
}];
// NOTE: MagicalRecord's setup is asynchronous, so at this point the default persistent store is still probably NIL!
// Uncomment the following line if you want to check it.
NSLog(@"%@", [MagicalRecord currentStack]);
// The persistent store COORDINATOR is however fully setup and can be accessed
// Uncomment the following line if you want to check it.
NSLog(@"Store coordinator at this point %@", [NSPersistentStoreCoordinator MR_defaultStoreCoordinator]);
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Finally, store change notifications must be observed. Without these, your app will NOT function properly!
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This notification is issued only once when
// 1) you run your app on a particular device for the first time
// 2) you disable/enable iCloud document storage on a particular device
// usually a couple of seconds after the respective event.
// The notification must be handled on the MAIN thread and synchronously
// (because as soon as it finishes, the persistent store is removed by OS).
// Refer to Apple's documentation for further details
[[NSNotificationCenter defaultCenter] addObserverForName:NSPersistentStoreCoordinatorStoresWillChangeNotification
object:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]
// queue:nil // Run on the posting (i.e. background) thread
queue:[NSOperationQueue mainQueue] // Run on the main thread
usingBlock:^(NSNotification *note) {
// For debugging only
NSLog(@"%s notificationBlockWillChange:%@, isMainThread = %i", __PRETTY_FUNCTION__, note, [NSThread isMainThread]);
// Disable user interface with setEnabled: or an overlay
// NOTE: Probably not crucial since the store switch is almost instantaneous.
// I only hint it here by changing the tint color to red.
self.window.tintColor = [UIColor redColor];
// Save changes to current MOC and reset it
if ([[NSManagedObjectContext MR_defaultContext] hasChanges]) {
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}
[[NSManagedObjectContext MR_defaultContext] reset];
// TODO: Drop any managed object references here
}];
// This notification is issued couple of times every time your app starts
// The notification must be handled on the BACKGROUND thread and asynchronously to prevent deadlock
// Refer to Apple's documentation for further details
[[NSNotificationCenter defaultCenter] addObserverForName:NSPersistentStoreCoordinatorStoresDidChangeNotification
object:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]
queue:nil // Run on the posting (i.e. background) thread
usingBlock:^(NSNotification *note) {
// For debugging only
NSLog(@"%s notificationBlockDidChange:%@, isMainThread = %i", __PRETTY_FUNCTION__, note, [NSThread isMainThread]);
// This block of code must be executed asynchronously on the main thread!
dispatch_async(dispatch_get_main_queue(), ^{
// Recommended by Apple
[[NSManagedObjectContext MR_defaultContext] reset];
// Notify UI that the data has changes
// NOTE: I am using the same notification that MagicalRecord sends after merging changes
[[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCDidCompleteiCloudSetupNotification object:nil];
// Re-enable user interface with setEnabled: or removing the overlay
// NOTE: Probably not crucial since the store switch is almost instantaneous.
// I only hint it here by changing the tint color back to default.
self.window.tintColor = nil;
});
}];
}
但是如何将本地商店的旧数据转移到iCloud? 谢谢你的帮助。