简体   繁体   中英

How to stop user losing Core Data file after iOS update

I have an ios app on the app store which in the latest upgrade, the users have lost their data. For this release, I had to change the location of the user's sqlite file (long story). The original file was in \\Documents\\myApp.sqlite. With the upgrade, I copy it to \\Documents\\myAppDB.sqlite. The code is mostly shown below where the store url points to the new location and legacyStoreURL to the old location.

BOOL stillUsingLegacyStore = YES;
stillUsingLegacyStore = !([fileManager fileExistsAtPath:[[self storeURL] path]]);
 if ([fileManager fileExistsAtPath:[[self legacyStoreURL] path]]  && stillUsingLegacyStore==YES)
 {
     _store = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType
                                       configuration:nil
                                                 URL:[self legacyStoreURL]
                                             options:options
                                               error:&error];
     if (!_store)
     {
         FLOG(@"Error adding OLD persistent store to coordinator %@\n%@",
              [error localizedDescription], [error userInfo]);
         return;
     }

     _store = [_coordinator migratePersistentStore:_store
                                           toURL:[self storeURL]
                                         options:options
                                        withType:NSSQLiteStoreType
                                           error:&error];
     if (!_store)
     {
         FLOG(@"Error adding OLD persistent store to coordinator %@\n%@",
              [error localizedDescription], [error userInfo]);
         return;
     }
 }
 else
 {
     _store = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType
                                         configuration:nil
                                                   URL:[self storeURL]
                                               options:options
                                                 error:&error];
     if (!_store)
     {
         NSLog(@"Failed to add store. Error: %@", error);abort();
         return;
     }
        else
        {
            NSLog(@"Successfully added store: %@", _store);
        }

}

I cannot reproduce the problem on my own devices. Since I decided not to delete the original file, I am hoping that I can do an upgrade which would allow the user to restore the old file. However, I would really like to be able to understand and reproduce the problem, before attempting this. Can anyone work out why this might have gone wrong, and why I haven't been able to reproduce it.

您是否复制了-wal和-shm文件?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM