Application with master data data storage failure

I use Core data to enter song data into the database. I have 3 views for this. First, select the name of the song and its detailed view to save the data in the database .. and the third view displays the saved songs. My application saves data and sometimes gives an exception.

I found when it gives an exception. If I select a song and save it to the database, it is saved correctly. But when I first turn to the 3rd look. Songlistviewcontroller, and then open the songs and try to save their details, they give an exception in the save line.

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

Here is my code for saving 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 on this issue .. and cannot understand why this is happening.

Previously, my application stream was .. SongsListviewController-> Songs-> SaveSongs

And it worked great ... for that.

Please, help

+4
source share
1 answer

There are some interesting ways that Core Data becomes "vile" on you and can store links to things that no longer exist.

In this case, the suspect was NSFetchedResultsController.

You install the NSFetchedResultsController delegate β€” and receive an update later β€” except that your delegate instance was no longer used to process this update.

Some background:

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

It is very important to make sure that you use any weak links when you install such delegates (pre-ARC), because they are not automatically zero and they can break if you leave them in place.

The solution is simple; set the delegate to zero when your instance is freed.

+6
source

Source: https://habr.com/ru/post/1379593/


All Articles