views:

123

answers:

1

I copied a filled, existing sqlite3-file to my project; it is found (I check that with

- (void)createEditableCopyOfDatabaseIfNeeded {
    NSLog(@"Checking if we have to create editable copy of database");

    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"hvw.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    NSLog(@"Creating editable copy of database");
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"hvw.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

I do not come to the "Creating editable copy of database"-part, so the file is there and found.

But my fetch does not return any results: NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Game" inManagedObjectContext:managedObjectContext];

[request setEntity:entity];

NSError *error;

NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

No error is thrown, but the resulting array is empty...

edit

I replaced the line

NSString *documentsDirectory = [paths objectAtIndex:0];

by

NSString *documentsDirectory = [self applicationDocumentsDirectory];

and in the log file I see error while trying to save or read objects:

Operation could not be completed. (Cocoa error 256.)
+1  A: 

Was this SQLite database created using Core Data? You can't just use any SQLite database you want for your persistent store, it has to have the exact internal format that Core Data uses.

If this is a standard SQLite database that you want to bring across to Core Data, my recommendation is to write a migration method that takes in the SQLite and creates Core Data managed objects from that, setting up all of the appropriate relationships. When the migration operation has completed, you can write out the resulting persistent store to disk and use it for Core Data within your application from then on. If you're more adventurous, you can have this migration take place on a background thread, passing notifications to add managed objects to the main thread's managed object context as they are parsed from SQLite.

Brad Larson
I assume that my sqlite-file has the structure it needs...I did it according to http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/
swalkner
If I understand that post correctly, he created a blank SQLite database from Core Data, then populated the data in it by hand? That doesn't guarantee that relationships and other data for your elements will be in the format that Core Data expects.
Brad Larson
hmm... but how do I use a prepopulated database, then?
swalkner
I created a Mac app that also uses Core Data. I use the Mac app to populate the DB, then move the file. Core Data is compatible between platforms. More: http://stackoverflow.com/questions/928177/provide-base-data-for-core-data-application/928390#928390
Hunter
For another test of your data (or even a means of entering it by hand), I'd suggest trying out the Core Data Editor application: http://christian-kienle.de/CoreDataEditor/
Brad Larson
@Hunter - but I'd like to fill the database on a server (using java) and then the iPhone App can fetch the sqlite-file from there... that should be possible too, shouldn't it?
swalkner
It's possible, but you'll need to follow the internal Core Data SQLite format exactly, which will require some reverse-engineering on your part. If this could somehow be generated on the backend by a Mac application using Core Data, or the data could be sent in another format and entered into the Core Data database by your application, things would be much easier.
Brad Larson
The internal CD format is not public and it is subject to change without notice. You may be able to imitate it but you risk it breaking on every revision of the OS. You need to either pre-populate with Core Data or do an import. Trying to "fake it" is not going to work for long, if you ever do get it to work. I suggest following Brad's advice and do an import.
Marcus S. Zarra