开发者

Including Core Data in app bundle

开发者 https://www.devze.com 2023-01-23 07:56 出处:网络
I\'m not sure that I completely understand how Core Data works on iOS. I have a large JSON file which I have parsed and imported into core data. This generates an sqlite file. I need t开发者_如何转开发

I'm not sure that I completely understand how Core Data works on iOS. I have a large JSON file which I have parsed and imported into core data. This generates an sqlite file. I need t开发者_如何转开发his file to be included with the app but every time I delete the app from the device - I have to run the JSON parse script again to create a new sqlite file on the device. I want to be able to exclude the JSON file from the application bundle and dont want to run the parsing script on first use.

How do I go about doing this? Am I doing something wrong?


You will need to create the sqlite file (using your app if you like), then copy it into your project and deploy it with the app. You will also need to add some code to move the file into your documents directory when your app runs for the first time. It can be a simple if file doesn't exist then copy it script.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DB = [[paths lastObject] stringByAppendingPathComponent:@"myDB.sqlite"];
if (![fileManager fileExistsAtPath:DB]) {
    NSString *shippedDB = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myDB.sqlite"];
    [fileManager copyItemAtPath:shippedDB toPath:DB error:&error];
    }

I use this method to ship out pre-built sqlite files, although I haven't used it when CoreData is managing the sqlite file.

0

精彩评论

暂无评论...
验证码 换一张
取 消