I am not that familiar with core data so im wondering if its possible to load different pre-populated sqlite db's into core data and how the performance would be?
I have a client who is storing their data in multiple sqlite db's on a remote server. Each sqlite db would basically represent a group in the app and there could be anywhere from 10-100 different groups each with their own small sqlite db. All the db's have the same schema and table structure, could be updated remotely and only one db would be used at a time.
Is it possible or even advisable to use core data in this instance? I could either:
- Download the sqlite db from a remote server and then load each db as needed accessing the db directly using sql or,
- Download the sqlite db and use core data to load each db and work with it that way? Although fr开发者_开发技巧om what i have read core data using a bunch of proprietary tables, etc to track the db so it isn possible to just swap in different db's even though they have the same schema?
Any ideas? thx
Yes, I do this right now. I can give you more examples if needed but here's what I do:
- Create the database via sqlite3 on the Mac.
- Place the database in the App's bundle
- On app launch, if the database is not in the app's Documents folder, copy it from the bundle to there
- Use the database from the App's bundle.
Alternatively, you could download from the web instead of doing steps 2 and 3 - but I'd only do this if it was very large, and would have the disadvtantage of maybe not being immediately available when the app loads.
Inside applicationDidFinishLaunching:
NSFileManager *fm = [[NSFileManager alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *mapPath = [documentsDirectory stringByAppendingPathComponent: @"datafile.sqlite" ];
if (![fm fileExistsAtPath:mapPath]) {
// Does Not Exist!!
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"datafile" ofType:@"sqlite" inDirectory:@"datafiles"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData)
[fm createFileAtPath:mapPath contents:myData attributes:nil];
}
In the last weeks my colleagues and I were discussing the best way of pre-populating a CoreData store on a server. We found the Objective-C web framework Bombax and
want to give it a try soon.
The idea is, that the Bombax server can use all available Frameworks, including CoreData.
The downside: the server has to be a mac.
精彩评论