2

Okay - I might be losing my mind and I hope I am, because the alternative seems worse right now. I upgraded to Xcode 9.4 and all of a sudden my Core Data app started acting crazy. I think I've found something, but it makes no sense to me.

All of a sudden, in prepareForDeletion, it seems that the object being deleted has had it relationships already deleted! When I go to log the objects that are in relationship to the object, there are no objects!

For example, if my object being deleted is Department, and I want to perform an action on all the Department's Employees before deleting the Department.

- (void)prepareForDeletion {

    NSLog(@"prepareForDeletion");
    NSMutableArray *employees = [[NSMutableArray alloc] initWithArray:[self.employees allObjects]];
    NSLog(@"employees:%@",employees);
    for (Employee *employee in employees) {
        NSLog(@"employee.name:%@",employee.name);
    }
}

The two NSLogs show that there are no employees in the Department by the time we are in the prepareForDeletion method. However, when I run the same code right before I delete the object, all of the Department's employees are listed!

This seems very, very wrong. It basically kills my app right now, since I use this so much. So someone please tell me - am I going crazy? Did I miss something here? Or is this actually an issue with Xcode 9.4?

Am I right in assuming that object relationships should NOT be deleted already, at this point?

SAHM
  • 4,078
  • 7
  • 41
  • 77
  • Have you checked possible breaking changes in XCode 9.4 release notes? E.g. modification for NSObjects are: https://developer.apple.com/documentation/objectivec/nsobject?changes=latest_minor&language=objc Makes sense to check the changes XCode 9.4 might have affected your app once. Because, if it was working well <9.4, and without any change is not working in 9.4, I am thinking may be because of some of these changes. – Manganese Jun 15 '18 at 06:47
  • And yes you are right in saying that object relationship should NOT be deleted already. It is after the call to `prepareForDeletion()` that they are deleted. Try checking for committed values within this function call. `committedValues(forKeys:)` Refer to https://developer.apple.com/documentation/coredata/nsmanagedobject/1506674-preparefordeletion?language=objc for explanation. – Manganese Jun 15 '18 at 06:55
  • Can't tell anything for sure other than the above, unless I see more of your code for deletion and other CoreData relevant ones. – Manganese Jun 15 '18 at 06:58
  • I code in Swift. I tried printing my objects and relationships within `prepareForDeletion()` and it worked fine. Meaning, relationship is not destroyed within the call to this function. My code for calling: ```override func prepareForDeletion() { let request: NSFetchRequest = Object.fetchRequest() let match = try? container?.viewContext.fetch(request) for m in match!! { print(m.countCorrect) print(m.game) } }``` – Manganese Jun 15 '18 at 07:15

0 Answers0