Master data moves data to a shared container

I have an already published application that uses basic data.
Now I want to add support for a set of watches and today extensions.

I need to transfer the main data to a common container without losing the previously saved user data, how can I do this in the best way?

+6
source share
2 answers

You can transfer the stack of master data. A more complete answer can be found here , but a short version:

  • Check if an old non-group copy of the data exists.
  • If so, configure the Core Data stack using this file. Then use migratePersistentStore:toURL:options:withType:error: to move it to a new location. Then delete the old copy.
  • If the old version does not exist, simply install Core Data with a new copy, as usual.

(The problem with Stephen's answer is that it assumes that the Core Data stack is the only SQLite file, which is not always true.)

+4
source

This is how I moved the master data to a shared container in my application. I do this when the application starts.

 NSUserDefaults* sharedDefs = [GPMapCore sharedCore].sharedUserDefaults; if (![sharedDefs boolForKey:@"CoreDataMovedToExtension"]) { NSURL* oldLocation = GET_LOCATION_OF_CORE_DATA_SQLITE_FILE(); NSURL* newLocation = GET_LOCATON_TO_MOVE_THE_SQLITE_FILE_TO(); if ([[NSFileManager defaultManager] fileExistsAtPath:[oldLocation filePathString]]) { //Check if a new file exists. This can happen when the watch app is run before //Topo Maps+ runs and move the core data database if ([[NSFileManager defaultManager] fileExistsAtPath:[newLocation filePathString]]) { [[NSFileManager defaultManager ] removeItemAtURL:newLocation error:nil]; } [[NSFileManager defaultManager] moveItemAtURL:oldLocation toURL:newLocation error:nil]; } [sharedDefs setBool:YES forKey:@"CoreDataMovedToExtension"]; [sharedDefs synchronize]; } 
+2
source

Source: https://habr.com/ru/post/985348/


All Articles