Permalink
Browse files

If a cloud reset occurs (e.g. user changes accounts), the app will no…

…w migrate the cloud store to local store options. Previously this was not done.
  • Loading branch information...
1 parent cc81e93 commit 46dfad5fdffe43f178ea067adaf9ab76f1f669c0 @drewmccormack committed Feb 5, 2013
Showing with 36 additions and 3 deletions.
  1. +36 −3 iCloudCoreDataTester/AppDelegate.m
@@ -66,8 +66,15 @@ -(id)init
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
[self checkIfCloudDataHasBeenReset:^(BOOL hasBeenReset) {
- if ( hasBeenReset ) [self disableCloudAfterResetAndWarnUser];
- [self setupCoreDataStack:self];
+ if ( hasBeenReset ) {
+ [self disableCloudAfterResetAndWarnUser];
+ [self convertCloudStoreToLocalOnlyStore:^{
+ [self setupCoreDataStack:self];
+ }];
+ }
+ else {
+ [self setupCoreDataStack:self];
+ }
}];
}
@@ -488,6 +495,30 @@ -(void)migrateStoreToCloud:(void(^)(void))completionBlock
});
}
+-(void)convertCloudStoreToLocalOnlyStore:(void(^)(void))completionBlock
+{
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
+ // Store options for migration
+ NSDictionary *localOnlyOptions = @{
+ NSMigratePersistentStoresAutomaticallyOption: @YES,
+ NSInferMappingModelAutomaticallyOption: @YES,
+ };
+ NSDictionary *cloudOptions = @{
+ NSMigratePersistentStoresAutomaticallyOption: @YES,
+ NSInferMappingModelAutomaticallyOption: @YES,
+ NSPersistentStoreUbiquitousContentNameKey: MCCloudMainStoreFileName,
+ NSPersistentStoreUbiquitousContentURLKey: self.cloudStoreURL,
+ NSReadOnlyPersistentStoreOption: @YES
+ };
+
+ // Migrate in place
+ [self migrateStoreAtURL:self.localStoreURL fromOptions:cloudOptions toOptions:localOnlyOptions];
+
+ // Complete
+ dispatch_async(dispatch_get_main_queue(), completionBlock);
+ });
+}
+
-(void)removeCloudData
{
NSURL *storeURL = self.cloudStoreURL;
@@ -539,7 +570,9 @@ -(void)cloudResetSentinelDidDetectReset:(MCCloudResetSentinel *)sentinel
if ( !usingCloudStorage ) return;
[self tearDownCoreDataStack:self];
[self disableCloudAfterResetAndWarnUser];
- [self setupCoreDataStack:self];
+ [self convertCloudStoreToLocalOnlyStore:^{
+ [self setupCoreDataStack:self];
+ }];
}
-(void)disableCloudAfterResetAndWarnUser

0 comments on commit 46dfad5

Please sign in to comment.