2014-01-10 167 views
0

我有一個已經存在的由dropbox同步的核心數據應用程序,我想在iCloud上遷移(使用iCloudCoreDataStack)。 並有2個設備(iPhome和iPad)裏面有相同的數據。將核心數據遷移到iCloud

我更新了icloud的新應用程序的iPad應用程序,一切工作正常,同步化工作。

遷移代碼:

// 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]; 



} 

當我更新iPhone,所有核心數據項是重複的,我下了原因:我和合並現有的商店與icloud的存儲與相同的信息。 如果你有想法,並避免...

感謝

回答

0

等待合併完成(監聽核心數據的通知),然後檢查店裏看看數據是否已經在那裏。