0
votes

I have an already existing core data apps synchonized by dropbox and I want to migrate on iCloud (with iCloudCoreDataStack). And have 2 devices ( iPhome and iPad) with the same data inside.

I update the ipad apps to the new apps with icloud, everything work fine and synchonisation works.

Migration code :

// check old if old Db exist

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Livre_de_Cave.sqlite"];

// migrate old persistentStoreCoordinator to icloud and delete it

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] ) {

    NSLog(@"oldURL");




    NSURL* oldStoreURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
    [oldStoreURL URLByAppendingPathComponent:@"Livre_de_Cave.sqlite"];
    oldStoreURL =[oldStoreURL URLByAppendingPathComponent:@"Livre_de_Cave.sqlite"];


    NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption:@YES,
                              NSInferMappingModelAutomaticallyOption:@YES,
                              NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"}
                              };

    NSPersistentStore *store = [self.managedObjectContext.persistentStoreCoordinator  addPersistentStoreWithType:NSSQLiteStoreType

                                                      configuration:nil

                                                                URL:oldStoreURL

                                                            options:options

                                                              error:&error];



 [self.managedObjectContext.persistentStoreCoordinator  migratePersistentStore:store
                                                                         toURL:self.storeURL
                                                                       options:@{ NSPersistentStoreUbiquitousContentNameKey : @"iCloudStore"}
                                                                                  withType:NSSQLiteStoreType
                                                                         error:&error];

    //[store removePersistentStore:store error:&error];
    [[NSFileManager defaultManager] removeItemAtPath:oldStoreURL.path error:&error];


} else {

    NSLog(@"newURL");
    [self.managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                                       configuration:nil
                                                                                 URL:self.storeURL
                                                                             options:@{ NSPersistentStoreUbiquitousContentNameKey : @"iCloudStore" }
                                                                               error:&error];



}

When I update the iphone, all core data entries are duplicates, I under why : I merge and existing store with the icloud store with the same information. If you have and idea to avoid that ...

Thanks

1

1 Answers

0
votes

Wait for the merge to complete (listening for the Core Data notifications) then check the store to see if the data is already in there.