0

I'm trying to use an NSFetchedResultsController(FRC) for my UITableViewController because I like the functionality that comes with a FRC as opposed to trying to manually manage my UITableView

My problem is that I need to have a filter on my table view, and my filter cannot easily be accomplished without using 'predicateWithBlock' which doesn't work with my FRC See Related Post

So what I want to do at this point is to create a context specifically for my FRC, then only add the objects I want to display in my FRC to this separate context. This way my FRC doesn't need to filter anything, it only needs to pull objects directly out of this temporary context.

So my question is how can I create this 'child' context? Is this even a good idea?

I've tried to create an empty context then insert objects from the main context, but it did not look trivial trying to manually copy NSManagedObjects from one context to another. I'm wondering if there's a better way to create a subset that I'm missing?

NSMangedObjectContext * myFRCContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

NSArray * managedFoobarObjectsToInsert = [myRealContext getFoobarObjects];

for(id managedObject in managedObjectsToInsert) {
    if([managedObject passesFilterConditions])
        //...now what?
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aeisys
  • 367
  • 1
  • 3
  • 13

1 Answers1

0

So my question is how can I create this 'child' context?

Well, a child context is a thing. Just create a context and set its parent to your original managed object context.

https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506529-parent

There is discussion at the top of the NSManagedObjectContext documentation about how a child context relates to its parent.

matt
  • 515,959
  • 87
  • 875
  • 1,141