0

I am experiencing problems with how I handle my Core Data NSManagedObjectContext.

I can create NSManagedObject in my NSManagedObjectContext, but I failed to save the value.

Here's what I got:

_lesson.title = _titleField.text;

int priority = [_priorityField.text intValue];
int difficulty = [_difficultyField.text intValue];
int time = [_timeField.text intValue];
int sortIndex = 0;
if ( time == 0 )
{
    sortIndex = 101;
}
else
{
    sortIndex = priority * ( difficulty / time );
}
_lesson.priority = [NSNumber numberWithInt:priority];
_lesson.difficulty = [NSNumber numberWithInt:difficulty];
_lesson.time = [NSNumber numberWithInt:time];
_lesson.sortIndex = [NSNumber numberWithInt:sortIndex];
NSError* error = nil;
[[(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext] save:&error];

Everything before the saving is working perfectly, I used NSLog to verify if each value is really saved in _lesson.

And _lesson is sent from here:

if ( [[segue identifier] isEqualToString:@"addLesson"] )
{
    LessonViewController* destination = [[LessonViewController alloc]init];
    Lesson* lesson = (Lesson*)[NSEntityDescription insertNewObjectForEntityForName:@"Lesson" inManagedObjectContext:_managedObjectContext];
    destination.lesson = lesson;
}
else if ( [[segue identifier] isEqualToString:@"editLesson"] )
{
    LessonViewController* destination = [[LessonViewController alloc]init];
    NSIndexPath* index = [_tableView indexPathForCell:(UITableViewCell*)sender];
    [_managedObjectContext deleteObject:[_lessonArray objectAtIndex:index.row]];
    Lesson* lesson = (Lesson*)[_lessonArray objectAtIndex:index.row];
    destination.lesson = lesson;
}

After debugging for two hours, I cannot find my error. Please help!

I will include my full code below:

https://www.dropbox.com/sh/eu62ie9svbbqdmm/u1hYUICfjy

That is my full source code. (I copied and pasted and created a mess. So, Dropbox!)

Thanks in advance.

Shane Hsu
  • 7,937
  • 6
  • 39
  • 63

1 Answers1

1

This line looks suspicious:

[_managedObjectContext deleteObject:[_lessonArray objectAtIndex:index.row]];

You delete the Lesson object before passing it to the LessonViewController, so that saving the context will delete that object from the store, and not save a (modified) object, as you probably intended.

It seems to me that you should just delete that line in your code.


ADDED: There is an error in your prepareForSegue method: You create a new view controller with

LessonViewController* destination = [[LessonViewController alloc]init];

Instead, you must use the destination view controller of the seque:

LessonViewController *destination = [segue destinationViewController];
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • It's a line of code from a previous method of handling editing. Before even deleting it, I really want to take a minute and say to myself "Wow, you're stupid." I will remove that line and try again. Thanks for reading that long code. – Shane Hsu Dec 21 '12 at 16:21
  • After realizing I am stupid. Another problem strikes me. The data is not being edited, so that line would not really execute for now as I'm not really trying to edit anything just yet. But you're right, that line should be deleted. – Shane Hsu Dec 21 '12 at 16:23
  • @ShaneHsu: Do you reload the table view in the ProjectViewController, when a lesson has been added? Otherwise you would not see the new object in the table view. – Martin R Dec 21 '12 at 16:26
  • The saving part. I can add a Lesson to the context with `insertNewObjectForEntityForName`, and pass that Lesson to my editing view. But when I save my edit, and wants to come back to the Table View, all the data is gone. They remain blank. The record is there, but it's empty. I will put my source code on Dropbox. – Shane Hsu Dec 21 '12 at 16:28
  • @ShaneHsu: OK, I can have a look at it. What about my "reload" question? – Martin R Dec 21 '12 at 16:29
  • I don't think I have too, it does that automatically when I do a modal segue back to the Project View. That's why the new data appears, but with blank value. Here's the link to the ZIP archive: https://www.dropbox.com/sh/eu62ie9svbbqdmm/u1hYUICfjy – Shane Hsu Dec 21 '12 at 16:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21529/discussion-between-shane-hsu-and-martin-r) – Shane Hsu Dec 21 '12 at 16:33