سؤال

I have two NSManagedObject (Person and Car) and a relationship "not inverse" between them. The relationship is not inverse.

I have two ViewControllers, the first one has a TableView that shows each instance of Person Object and the second has a Table View that shows every Car of that Person. The first View Controller shows the name of the Person and the number of Cars of that Person.

All works fine but my issue appear when I try to delete a Car of a Person. I think I remove the object correctly:

[_person removeCarssObject:car1];

But my issue really appear when being in the second view, which is where I remove the Car, and I go to the first view: The number of Cars doesn't refresh and the app crashes.

When I add a Car and I go to the second view it works and the 'number of cars' increases.

When I run the app again, the car is deleted. This is why I think that the way I delete the object is correct.

I've realized that the app works always that the number of cars is going to take value 0, otherwise the app crashes.

I hope you can help me, Thank you!

UPDATE:

I’ve changed my code, and now I reload the data of the table view in

- (void)viewWillAppear:(BOOL)animated{

This way I can see the proper data in the table view, but, only one second approx, later the app crashes with the log: “Thread 1: EXC_BAD_ACCESS(code=EXC_I386_GPFLT)"

All relationships in my "model data" have the Delete Rule property : Nullify. Maybe this is what is causing the crash.

هل كانت مفيدة؟

المحلول

Finally I have the issue solved. Enabling Zombie Objects I got more information about the crash. The app was crashing with the following message:

[ViewController tableView:canEditRowAtIndexPath:]: message sent to deallocated instance 0xaae64d0

I had that method like this:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
      return YES;
   }

Searching, finally I found that question in stackoverflow which says the same: tableView:canEditRowAtIndexPath: crash when popping viewController

The issue is solved adding the following code:

- (void)viewWillDisappear:(BOOL)animated {
   [super viewWillDisappear:animated];
   [table setEditing:NO];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top