praveen seela
praveen seela

Reputation: 518

Core Data managed object context save not working

I am trying to write data to persistent storage with managed object context. inserting simple static data. save is returning no errors but fetch is not returning any results. here is my code:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyDB" withExtension:@"momd"];`

NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSAssert(mom!=nil, @"Error in initializing Managed Object Model");

NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[moc setPersistentStoreCoordinator:psc];
[self setManagedObjectContext:moc];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [documentsURL URLByAppendingPathComponent:@"MyDB.sqllite"];

NSError *error  = nil;
NSPersistentStoreCoordinator *pscStore = [moc persistentStoreCoordinator];
NSPersistentStore *store = [pscStore addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];
NSAssert(store!=nil,@"Error in initializing PSC:%@\n%@",[error localizedDescription],[error userInfo]);

//insert
Schedule *schedule = [NSEntityDescription insertNewObjectForEntityForName:@"Schedule" inManagedObjectContext:moc];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *sessionEndDateReturned = [NSDate alloc];
sessionEndDateReturned = [dateFormatter dateFromString:@"24-04-2016"];
NSDate *sessionStartDateReturned = [NSDate alloc] ;
sessionStartDateReturned = [dateFormatter dateFromString:@"25-04-2016"];

//schedule.category = @"cat";
schedule.about = @"testabout";
schedule.endDate = sessionEndDateReturned;
schedule.eventId = @"e1";
schedule.sessionID = @"s1";
schedule.roomNumber = @"r1";
schedule.startDate = sessionStartDateReturned;
schedule.topic = @"topic1";
schedule.type = @YES;
NSLog(@"inserted objs:%@",moc.insertedObjects);
//save
NSError *insertError  = nil;
if(![moc save:&insertError]){
    NSLog(@"%@ %@",insertError,insertError.localizedDescription);
}

//fetch
NSFetchRequest *fetchReq = [NSFetchRequest fetchRequestWithEntityName:@"Schedule"];
NSError *fetchError = nil;
NSArray *result =[moc executeRequest:fetchReq error:&fetchError];
if(fetchError){
    NSLog(@"fetch Error: %@",fetchError.localizedDescription);
}
else
{
    NSLog(@"myFetch Result:%@",result);
}

can anybody tell me where i am going wrong?

Upvotes: 1

Views: 280

Answers (1)

praveen seela
praveen seela

Reputation: 518

everything is fine in the above code except fetching part. only small change is required. Save is happening fine, problem only with fetch. change the fetch execute with below line:

NSArray *result =[moc executeFetchRequest:fetchReq error:&error];

I was doing executeRequest instead of executeFetchRequest :|

Upvotes: 1

Related Questions