Friday, 16 August 2013

Core Data not saving objects persistently

Core Data not saving objects persistently

I'm new to Core Data and as such am not sure if I'm making a mistake. I've
downloaded some data from a REST API and it successfully saves the JSON
response to disk. I'm trying to process the data and save it persistently
using Core Data.
NSLog(@"inserted objects: %@", [managedObjectContext insertedObjects]);
[managedObjectContext performBlockAndWait:^{
NSError *error = nil;
if (![managedObjectContext save:&error]) {
NSLog(@"Unable to save context for class %@", className);
} else {
NSLog(@"saved all records!");
}
}];
I've successfully processed the JSON and added it to an
NSManagedObjectContext. In the first line, it shows that I've successfully
attempted to insert 2 objects.
inserted objects: {(
<User: 0xa259af0> (entity: User; id: 0xa259b70
<x-coredata:///User/t44BB97D0-C4B4-4BA6-BD25-13CEFDAE665F3> ; data: {
email = "vishnu@vishnuprem.com";
experience = "2013-07-20";
"first_name" = Vishnu;
id = 2;
"job_title" = Developer;
"last_name" = Prem;
location = "";
"phone_number" = "+6590091516";
"profile_pic" = "";
"thumbnail_profile_pic" = "";
"user_id" = 2;
}),
<User: 0xa25e460> (entity: User; id: 0xa25e4c0
<x-coredata:///User/t44BB97D0-C4B4-4BA6-BD25-13CEFDAE665F2> ; data: {
email = "sanchitbareja@gmail.com";
experience = "2013-07-20";
"first_name" = Sanchit;
id = 1;
"job_title" = Developer;
"last_name" = Bareja;
location = "";
"phone_number" = "+15106127328";
"profile_pic" = "";
"thumbnail_profile_pic" = "";
"user_id" = 1;
})
)}
When I attempted [managedObjectContext save:&error], it does so
successfully and print out "saved all records" as expected. However, when
I go to my application .sqlite file and check for added objects, I realize
that it hasn't added any objects to the db.
On app relaunch, I print out a list of objects that are already in the
database and it confirms that I've none saved yet.
Does anyone know what's going on and why I'm not able to save the data
persistently even though it looks like I've successfully created the
'User' objects that needs to be saved in the Core Data model.
EDIT:
here is where I create the NSPersistentStoreCoordinator
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the
application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory]
URLByAppendingPathComponent:@"RTModel.sqlite"];
NSError *error = nil;
NSLog(@"Test 1");
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
NSLog(@"Test 2");
if (![_persistentStoreCoordinator
addPersistentStoreWithType:NSSQLiteStoreType configuration:nil
URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate.
You should not use this function in a shipping application, although
it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current
managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically
something wrong with the file path. Often, a file URL is pointing
into the application's resources directory instead of a writeable
directory.
If you encounter schema incompatibility errors during development,
you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following
dictionary as the options parameter:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber
numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption, nil];
Lightweight migration will only work for a limited set of schema
changes; consult "Core Data Model Versioning and Data Migration
Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}

No comments:

Post a Comment