Migrate Persistant Store Crash

后端 未结 1 1520
独厮守ぢ
独厮守ぢ 2021-01-24 04:24

I have a switch that turns iCloud on or off for an iOS 7 app. iCloud sync works fine. When iCloud is on and I turn it off, I call this code:

- (void)migrateiClou         


        
相关标签:
1条回答
  • 2021-01-24 05:03

    Try This

    [psc removePersistentStore:currentStore error:nil];
    
    [psc migratePersistentStore:currentStore
                          toURL:self.store
                        options:options
                       withType:NSSQLiteStoreType
                          error:&error];
    
    [psc addPersistentStoreWithType:NSSQLiteStoreType
                      configuration:nil
                                URL:self.store
                            options:options /* options for local store */
                              error:nil];
    

    UPDATE: new solution

    NSPersistentStoreCoordinator * persistentStoreCoordinator = self.persistentStoreCoordinator;
    
    NSPersistentStore * persistentStore = [[persistentStoreCoordinator persistentStores] firstObject];
    
    NSMutableDictionary * localStoreOptions = [[self localPersistentStoreOptions] mutableCopy];
    
    [localStoreOptions setObject:@YES forKey:NSPersistentStoreRemoveUbiquitousMetadataOption];
    
    [[NSFileManager defaultManager] copyItemAtPath:persistentStore.URL.path toPath:[self storeURL].path error:nil];
    
    NSPersistentStoreCoordinator * newPersistentStoreCoordinator;
    
    newPersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    
    [newPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
    
                                                configuration:nil
    
                                                          URL:[self storeURL]
    
                                                      options:localStoreOptions
    
                                                        error:nil];
    
    [self setPersistentStoreCoordinator:newPersistentStoreCoordinator];
    
    [self setupManagedObjectContext]; //initialize new moc with new persistent store coordinator
    
    0 讨论(0)
提交回复
热议问题