CoreData can remove object from to many relationship

I've a very strange issue. This happens occasionally when I'm trying to remove an object from a to many relationship.



We have 2 objects, a DriverRoute which contains multiple DriverRouteDelivery objects. I try to remove one of the DriverRouteDelivery objects from the DriverRoute, but sometimes the item can't be found in the set even though it's there. I printed the output of lldb.



    <DriverRoute: 0x812041a0> (entity: DriverRoute; id: 0x7c0ed070 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/DriverRoute/p8> ; data: {
        changed = "2015-06-22 15:56:28 +0000";
        createdTimestamp = "2015-06-22 15:36:25 +0000";
        driver = "0x7f52f080 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/Driver/p1>";
        id = "4CFA3568-B95E-46D8-A9C7-5EC44BF1ADA8";
        returnRoute = "0x7bf15e70 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/Route/p23>";
        routeDeliveries =    (
            "0x7bf12930 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/DriverRouteDelivery/p24>",
            "0x7c663b80 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/DriverRouteDelivery/p14>",
            "0x7c663f60 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/DriverRouteDelivery/p21>"
        );
        routes =    (
            "0x7f5df860 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/Route/p33>",
            "0x7bf131f0 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/Route/p22>",
            "0x7bf12940 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/Route/p29>"
        );
        startAt = "2015-06-22 15:35:32 +0000";
        synced = "2015-06-22 15:56:29 +0000";
    })


and



    <DriverRouteDelivery: 0x81323d70> (entity: DriverRouteDelivery; id: 0x7bf12930 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/DriverRouteDelivery/p24> ; data: {
        delivery = "0x7c143b60 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/Delivery/p15>";
        route = "0x7c0ed070 <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/DriverRoute/p8>";
        sortOrder = 1;
    })


It's pretty obvious that the Set routeDeliveries contains the object <x-coredata://E8AAE873-94B7-42F4-A39A-3A8D7C567747/DriverRouteDelivery/p24> but if I try e.g. [routeDeliveries containsObject:routeDelivery] it occasionally return NO or if I try to remove the object using the CoreData accessor removeRouteDeliveriesObject: it simply doesn't work, it still contains the object after the call, the printed output above shows it's there.



Both objects are in the same context and the context is used on the correct queue.


Any one any ideas, what might cause this behaviour?

What can cause that behavior?

1. The mistaken idea that CoreData accessors are safe to use off-queue and that you only need to ensure that mutators are on-queue.

2. Overriding "harmless" methods on CoreData objects.

I've a parent-child queue setup. The parent context is initialised with NSPrivateQueueConcurrencyType and the child context is initialised with NSMainQueueConcurrencyType. The managed objects are loaded from the child context on the main queue, manipulated there and saved there. I didn't override any methods on any CoreData objects.

Unchanged is the fact that you're presenting the symptom of using a managed object outside of the proper queue while claiming that your code is all written to use the proper queue.


Or, if you're using Objective-C, you're not being careful and your occasional

[routeDeliveries containsObject:routeDelivery]

is returning NO being it is being performed while routeDeliveries is null.

CoreData can remove object from to many relationship
 
 
Q