How to put an existing sqlite file in <Application_Home> / Library /?

My application uses basic data. I ran the application in a simulator that successfully created and populated the corresponding sqlite file. Now I want this existing sqlite file to be on the device itself and be part of my application. I found the sqlite file created by the simulator in / Library / Application Support / iPhone Simulator / 6.0 / Applications / identifier / Documents / myapp.sqlite and dragged it into Xcode. This added it to my application package, but not in the corresponding directory (as a result of which the sqlite file can be read but not written).

From reading about the file system, I believe that the best place to place the sqlite file would be in the user database directory under Application_Home / Library /. It seems I cannot do this in Xcode, and despite the search, I cannot figure out how to do the following:

(1) Create a subdirectory called "Database" in Application_Home / Library /? (2) Transfer the sqlite file to my newly created "Database" directory?

Thanks a lot @ Daij-Djan for his answer below.

One more question: the path to the sqlite file will be used by the permanent storage coordinator. Now, depending on the size of the sqlite file, it may take some time to copy or move. How can you make sure that the sample code provided by @ Daij-Djan is executed and completed before the permanent repository coordinator tries to reference the sqlite file?

Thanks for any help in advance.

+4
source share
1 answer

mock code

in appDidFinishLaunching .. maybe

dbpath = PATH_YOU_WANT if(!file.exists_at:dbpath) { copy(dbinbundle, dbpath); } 

in objC: (recorded built-in, the possibility of typos / errors)

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { NSString *lib = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *dbs = [lib stringByAppendingPathComponent:@"Database"]; NSString *file = [dbs stringByAppendingPathComponent:@"db.sqlite"]; if(![[NSFileManager defaultManager] fileExistsAtPath:file]) { id dbInBundle = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite"]; [[NSFileManager defaultManager] createDirectoryAtPath:dbs withIntermediateDirectories:YES attributes:nil error:nil]; [[NSFileManager defaultManager] copyItemAtPath:dbInBundle toPath:file error:nil]; } .... } 
+4
source

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


All Articles