0

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.

Madhubalan K
  • 357
  • 5
  • 21
  • 1
    You are effectively adding the data to the cloud twice, which is why you get duplicates. The advised way to handle this is to de-duplicate your data after a sync. A Google search will give you some code to do that. Here's one: http://www.atomicbird.com/blog/icloud-complications-part-2 – Drew McCormack Jul 17 '15 at 19:04
  • Hi @DrewMcCormack thanks for reply. i have tried that also, it tells deduplicate within storedidimport:. It works fine when i have two device. If i have only one device storedidimport: method is not called and datas got duplicated. – Madhubalan K Jul 18 '15 at 13:44
  • Hmm, are you sure? I would have thought NSPersistentStoreDidImportUbiquitousContentChangesNotification would be posted whenever a background change was made to the store for sync. I would think that if you dedupe there, it should always work. – Drew McCormack Jul 22 '15 at 10:24
  • @DrewMcCormack while change store from local to iCloud (while using single device) NSPersistentStoreDidImportUbiquitousContentChangesNotification is not called(this will call after entry changed by iCloud sdk right?) . Now i have writ de-duplication while fetch data. I knew it is not a good way but it works. Do you have any better idea? – Madhubalan K Jul 22 '15 at 10:32

1 Answers1

0

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

Madhubalan K
  • 357
  • 5
  • 21