Reputation: 115
I am using core data in my source-code for data persistence. The framework is being used to persist some preferences in my RSS feed app across different devices all linked to the same iCloud account.
-(id)init{
if (self= [super init]) {
model= [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *psc= [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSError *error= nil;
NSString *dbPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[dbPath stringByAppendingPathComponent:@"feed.db"];
NSLog(@"%@",dbPath);
NSURL *dbURL= [NSURL fileURLWithPath:dbPath];
if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:dbURL options:nil error:&error]) {
[NSException raise:@"Open failed" format:@"Reason: %@",[error localizedDescription]];
}
context= [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];
[context setUndoManager:nil];
}
return self;
}
Error in the console—
2015-05-08 23:15:03.963 Nerdfeed4[1023:22906] CoreData: error: - addPersistentStoreWithType:SQLite configuration:(null)
URL:file:///Users/Rahul/Library/Developer/CoreSimulator/Devices/28EDC37B-E3AA- 442F-A1B1-72AD385563D5/data/Containers/Data/Application/BF11CCC0-0B2C-4537-BAB6- E27E2C84CC4C/Documents/ options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"
UserInfo=0x7a092c70 {NSUnderlyingException=unable to open database file,
NSSQLiteErrorDomain=14} with userInfo dictionary {
NSSQLiteErrorDomain = 14;
NSUnderlyingException = "unable to open database file";
}
2015-05-08 23:15:03.977 Nerdfeed4[1023:22906] *** Terminating app due to uncaught exception 'Open failed', reason: 'Reason: The operation couldn’t be completed. (Cocoa error 256.)
Clearly from the error,one can figure out that the execution stops at the "if(![psc addPersistentStoreWithType:.. configuration:.. URL:.. options:..error]) " because this message returns 0 for the exception to be thrown. How do i resolve this.. Pls assist. I do a clean build each time before the project is run.
Upvotes: 0
Views: 1469
Reputation: 21536
The stringByAppendingPathComponent
method does not mutate the receiver (dbPath
), it returns the new value, so this line:
[dbPath stringByAppendingPathComponent:@"feed.db"];
does nothing. You need to assign the return value to a new path string, and use that in the subsequent code:
NSString *newPath = [dbPath stringByAppendingPathComponent:@"feed.db"];
Upvotes: 0
Reputation: 80271
Delete the app from the simulator and restart. As was pointed out by other posters, you most likely change the database schema and now have an incompatible database file.
Upvotes: 1