When my sqlite database is created using a core data model, at this line:
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
I get an error:
NSUnderlyingException=I/O SQLite error code:1, 'no such table: Z_METADATA'
Any idea how to fix this? I've been trying for days. The database is created and copied into the documents directory on my device.
Note:
If I DELETE the application, rebuild, and install on my device - the .sqlite file is dated two days ago and the .mom file is dated yesterday. Is the database not recreated at compile time if necessary? I have no .sqlite file in my project, just the .xcdatamodel.
Thanks for your time.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persisten开发者_C百科tStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
//\MOVES DATABASE TO NEW LOCATION
NSString *storePath = [documentsDirectory stringByAppendingPathComponent:@"myShoeDatabase.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn’t exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"myShoeDatabase" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
}
enter code here
Please start with this basic sanity check: Bring the database to your Mac, open it with sqlite3.exe, and see if the table is present.
If it is, then your program is pointing to the wrong database. If it is not, then your table was never created (or the database is corrupt as hell, but that would be surprising).
Core Data seems to look for the metadata first when it opens a store so a complaint about the Z_METADATA usually indicates a corrupted file or a file in the wrong format. I would suspect either a problem with the copied file (permission, corruption etc) or a problem with the options
for the store.
I would suggest:
- Open the store as readonly in the app bundle i.e. without copying it and see if it opens fine. If it does, then the problem is in the copying.
- Provide an error to the copy method and log its return. The copy might be subtly failing.
- Get the size of the copied file. The file might end up in the directory but empty.
- Pass a
nil
options dict to make sure that is not the problem.
First, you need to catch these errors and at least report them:
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
Also, that method returns a BOOL of success or failure, you should be watching for that as well.
Beyond that, assuming the sqlite file is valid, there is nothing overtly wrong with the code. Can you duplicate this in a sample application? If so, you can post a link to it in your question and I will be happy to tinker around with it. If it turns out to be an Apple bug you will then have the test case ready for your radar. If it doesn't duplicate in a test then you can compare the differences to see what is wrong in the original application.
精彩评论