NSPredicate not executed

前端 未结 1 1032
南笙
南笙 2020-12-30 11:18

This is quite funny. In my application I create thousands of entry in the database (in another thread, I\'m using MagicalRecord). Everything seems working fine (from a backg

相关标签:
1条回答
  • 2020-12-30 11:51

    I had this same issue, this is what I figured out, and how I resolved it.

    Magical Record has a root NSManagedObjectContext as a parent of the default NSManagedObjectContext. When I create an NSFetchedResultsController in the default context, everything seems to work fine, just like you.

    The problem is that all the new NSManagedObject's come back with their still-temporary ObjectID's. So, in my case, I was using an NSPredicate to scope a query on an associated table. I didn't just call the association method because I didn't want to load everything into memory and wanted NSFetchedResultsController to handle changes, for me.

    With the temporary ObjectID the query finds zero results and that's exactly what it displays.

    Apparently the child context (default) doesn't get the benefit of the transformation to non-temporary ID's, even though it's been persisted to the backing store.

    Even worse badness happened when I tried to force the issue with obtainPermanentIDsForObjects:error:. Core Data complained that it could not satisfy a fault for my instance. Nevermind, there's no way it was actually a fault. Simply refreshing the object had no effect, either. I suspect this is a Core Data bug that hardly no one tickles because they just use the association methods to get an NSSet.

    My fix was to use the parent context for the NSFetchedResultsController, just like in this question, Magical Record, saving, and NSFetchedResultsController.

    I was already wrapping the default in a new child context upon edit and, therefore, copying the instances into that editing context with createInContext, so I didn't have to do any extra work beyond just adding .parentContext to the argument.

    Incidentally, this only ever happened on new instances of the source of the association. Once an instance was there from startup, it had a non-temporary ObjectID and never had the issue.

    0 讨论(0)
提交回复
热议问题