iProgrammer
iProgrammer

Reputation: 3107

Core data app is crashing while saving data

I am using Core data to enter song details in database. I have 3 views for this.. first is to select song name and its detail view to save details in database.. and third view is to display saved songs.. My App is saving data and sometimes giving exception.

I found when it is giving exception. If i select song and save it in database it is saving properly. But when I first go to 3rd view. Songlistviewcontroller and then open songs and try to save it details it gives exception on save line..

011-11-04 11:14:10.578 SongsWithLyrics[259:207] * -[SongsListViewController controllerDidChangeContent:]: message sent to deallocated instance 0x5b73b50

Here is my code to save songs

//save song details
- (IBAction)saveDetails:(id)sender {

    NSError *error;
    self.song = [NSEntityDescription insertNewObjectForEntityForName:@"Song" inManagedObjectContext:managedObjectContext];


    [song setValue:songTitleString forKey:@"songTitle"];
    [song setValue:albumNameText.text forKey:@"albumName"];
    [song setValue:artistNameText.text forKey:@"artistName"];
    [song setValue:albumGenreText.text forKey:@"albumGenre"];
    [song setValue:UIImagePNGRepresentation(artworkImageview.image) forKey:@"artworkImage"];


    if (![managedObjectContext save:&error])
    {
        NSLog(@"Problem saving: %@", [error localizedDescription]);
    }



    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Saved" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
    [alert release];

    [self.navigationController popViewControllerAnimated:YES];




}

I am stuck with the issue.. and can't understand why this is happening.

Earlier my application flow was .. SongsListviewController->Songs->SaveSongs

and It was working fine.. for that.

Please help

Upvotes: 4

Views: 570

Answers (1)

Steve
Steve

Reputation: 31662

There are several interesting ways Core Data gets "sneaky" on you and can keep references around to things that don't exist anymore.

In this case, an NSFetchedResultsController was the suspect.

You set the NSFetchedResultsController's delegate - and later got an update - except your delegate instance wasn't around anymore to process that update.

Some background:

If you set a delegate for a fetched results controller, the controller registers to receive change notifications from its managed object context. Any change in the context that affects the result set or section information is processed and the results are updated accordingly. The controller notifies the delegate when result objects change location or when sections are modified (see NSFetchedResultsControllerDelegate). You typically use these methods to update the display of the table view.

It's important to make sure you nil-out any weak references when you set delegates like this (pre-ARC) because they don't automatically zero - and they can break if left in place.

The solution is simple; set the delegate to nil when your instance is getting deallocated.

Upvotes: 6

Related Questions