Search code examples
objective-ccore-dataios8icloudicloud-drive

Data duplicate while migrate PersistentStoreCoordinator from local to iCloud


I am doing iCloud sync with core data works fine. Now i want to add on/off switch(migrate store). If i migrate store to iCloud to local works fine but if migrate back to iCloud datas getting duplicate. Even i tried to remove ubiquity container before migrate not helping. I could not deduplicate because "StoreDidImportUbiquitousContent" method not called after duplicate data.

Code below :

        // Migrate to local 

    -(NSURL *)localStoreURL
    {
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"LocalExpns.sqlite"];
        return storeURL;

    }

  - (void)moveStoreToLocal
{      
         NSError *error = nil;
            NSPersistentStoreCoordinator * persistentStoreCoordinator = self.persistentStoreCoordinator;

            NSPersistentStore * persistentStore = [[persistentStoreCoordinator persistentStores] firstObject];

            if([[NSFileManager defaultManager]fileExistsAtPath:[self localStoreURL].path])
            {
                NSLog(@"File exists");
                [[NSFileManager defaultManager] removeItemAtPath:[self localStoreURL].path error:& error];
                NSLog(@"Removing error = %@", error);

            }

            NSMutableDictionary *localStoreOptions = [[self localStoreOptions] mutableCopy];
            [localStoreOptions setObject:[NSNumber numberWithBool:YES] forKey:NSPersistentStoreRemoveUbiquitousMetadataOption];

           id result = [persistentStoreCoordinator migratePersistentStore:persistentStore toURL:[self localStoreURL] options:localStoreOptions withType:NSSQLiteStoreType error:&error];

}

Above code working fine.

// Migrate to iCloud back

// fileURL is local file url

-(NSURL *)storeURL
{
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Expns.sqlite"];
    return storeURL;
}


    - (void)moveStoreFileToICloud:(NSURL*)fileURL 
    {
     NSPersistentStoreCoordinator * persistentStoreCoordinator = self.persistentStoreCoordinator;

            id sourceStore = [[persistentStoreCoordinator persistentStores] firstObject];

            if (!sourceStore) {

                NSLog(@" failed to add old store");

            } else {
                NSLog(@" Successfully added store to migrate");

                bool moveSuccess = NO;
                NSError *error;


                NSLog(@" About to migrate the store...");
                // Now migrate the store using the iCloud options
                id migrationSuccess = [self.persistentStoreCoordinator migratePersistentStore:sourceStore toURL:[self storeURL] options:[self icloudStoreOptions] withType:NSSQLiteStoreType error:&error];

                if (migrationSuccess) {
                    moveSuccess = YES;
                    NSLog(@"store successfully migrated");

                    // Now delete the local file
                    if (shouldDelete) {
                        NSLog(@" deleting local store");
                        [self deleteLocalStore];
                    } else {
                        NSLog(@" not deleting local store");
                    }
                }
                else {
                    NSLog(@"Failed to migrate store: %@, %@", error, error.userInfo);
                }

            }
    }

Now data get duplicate what ever in before migrate. I tried to remove ubiquity container also not helping.


Solution

  • I have solved this problem by de-duplicate my db manually like this