I'm trying to make a little dictionary on my app by using Core Data.When you use a Master-Detail application, xxx.sqlite file gets created in the user's Documents folder. Now before I start the application for the first time, I changed the code as below because I wanted the xxx.sqlite file in the app:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
//NSURL *storeURL = [[self applicationDocumentsDirectory]
URLByAppendingPathComponent:@"CoreDataFileTest.sqlite"];
NSString* path= [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CoreDataTest.sqlite"];
NSURL* storeURL = [[NSURL alloc] initFileURLWithPath:path];
I thought the last two line would make a xxx.sqlite file in my app project folder. Actually, it didn't. But the app works fine. This means that the xxx.sqlite file is embedded in the app itself? Thank you for your time.
-
Remove app from simulator, and run app again.βhargavḯ– βhargavḯ2013年07月27日 04:59:42 +00:00Commented Jul 27, 2013 at 4:59
-
@Bhargavi Thanks for your advice. I removed my app from simulator and ran it, but I still can't find xxx.sqlite file in my app folder and the app still works! One thing I notices was that in the ' Cache' folder, I found these 3 files: Cache.db, Cache.db-shm and Cache.db-wal. Do they have something to do with this strange behavior?boochan224– boochan2242013年07月27日 09:14:01 +00:00Commented Jul 27, 2013 at 9:14
-
When I run find . -name "MyModel.sqlite" -print command on the terminal, I find 'xxx-xxx-xxx.../<my app name>/xxx.sqlite. I don't get it???boochan224– boochan2242013年07月27日 09:38:11 +00:00Commented Jul 27, 2013 at 9:38
-
@Bhargavi Hi. After the comment above, I tested it on my iPhone, not on Simulator. It failed; this means that simulator caches the data and reads from it. Then I have decided to make a sqlite file in Documents first, later I'll move it to the app folder. Thanks again.boochan224– boochan2242013年07月29日 00:40:03 +00:00Commented Jul 29, 2013 at 0:40
2 Answers 2
If, as I understand your question, you would like to include a default database with your app (i.e. the .sqlite file will be included as an asset of your project, then you need to copy it from the app bundle into your documents directory at startup).
Then call this function from your didFinishLaunchingWithOptions method in your app delegate.
- (void) copyDefaultDB
{
// If we are running the app for the first time, then copy our default database across from our app directory
// into our user directory
NSString *filePath;
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex: 0];
NSFileManager *fileManager = [NSFileManager defaultManager];
filePath = [documentsDir stringByAppendingPathComponent: @"CoreDataFileTest.sqlite"];
// If the database already exists then return without doing anything
if (![fileManager fileExistsAtPath: filePath])
{
// Now copy the new shiny one in
NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"CoreDataFileTest.sqlite"];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath: filePathFromApp
toPath: filePath
error: nil];
}
}
2 Comments
Try with this code. Write this inside AppDelegate.m
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"ProjectManagement.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:nil error:&error]){
/*Error for store creation should be handled in here*/
}
return persistentStoreCoordinator;
}
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return managedObjectModel;
}
- (NSString*)applicationDocumentsDirectory{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
}