RxSwiftCommunity / RxCoreData

RxSwift extensions for Core Data
MIT License
164 stars 68 forks source link

Proposal for FetchedResultsControllerEntityObserver and FetchedResultsControllerSectionObserver #7

Closed serluca closed 7 years ago

serluca commented 8 years ago

Issue

I had a performance issue using the rx_entities method. The fetch request that I have to execute is heavy and it blocks the Main thread, and as expected the bottleneck is try self.frc.performFetch(). So I created another context with the concurrency type PrivateQueueConcurrencyType and I called rx_entities using the new context but nothing the try self.frc.performFetch() was still executed on the Main Thread because I was calling rx_entities on the Main Thread.

Proposal

What do you think about wrapping the try self.frc.performFetch() and sendNextElement() with performBlock? Or even better to schedule rx_entities on the context thread.

scotteg commented 8 years ago

Hi @serluca, Thanks for raising this issue. Another option might be using NSAsynchronousFetchRequest. I won't have time to work on this for the next several days. If you would like to try out your suggestions and/or using an async fetch request, please let me know what you find out.

serluca commented 8 years ago

Hi @scotteg, I'd be happy to implement the solution, when we have one. My first thought was NSAsynchronousFetchRequest but it seems that is not possible to use NSAsynchronousFetchRequest with NSFetchedResultsController so then is not possible to observe the Fetch Request for future updates.

Wrapping the try self.frc.performFetch() with performBlock works but I'm not sure is the best solution, because the user still needs to call rx_entities using a PrivateQueueConcurrencyType context, what do you think?

I was thinking more about a solution that is asynchronous by default, unfortunately NSAsynchronousFetchRequest it can't help, one idea I had was to observe the changes with NSFetchedResultsController on a child Context and merge the changes using NSManagedObjectContextDidSaveNotification, then every time the frc delegate calls controllerDidChangeContent we can then map the new objects using objectWithID and return the new objects on the given context.

serluca commented 8 years ago

Hi @scotteg

I'm having some issue with the pull request #8, it works great when new contents come in, but it has issues when old contents get updated. I found that this is an old bug, the issue I have is that as soon the contents get updated disappear from the NSFetchedResultsController. I'm working on a fix, unfortunately the fix described in the article doesn't work for me.

It also only works when the subscriberContext gets saved, instead the default NSFetchedResultsController behaviour it also observe changes that are not saved yet, this can be an issue too.

Let me know if you have any idea

serluca commented 8 years ago

Hi @scotteg any news on this?