RestKit / RestKit

RestKit is a framework for consuming and modeling RESTful web resources on iOS and OS X
Apache License 2.0
10.18k stars 2.1k forks source link

Cached Response Threading Violation #2218

Open cfis opened 9 years ago

cfis commented 9 years ago

The objects returned by cached responses are incorrect. They returned objects are in the privateContext of the request operation, and then are sent via callbacks to the main thread (and main context). This results in CoreData threading violations (just turn on -com.apple.CoreData.ConcurrencyDebug 1 to see).

The incorrect code is here:

https://github.com/RestKit/RestKit/blob/development/Code/Network/RKManagedObjectRequestOperation.m#L594

This code was originally correct, but was changed by this commit three years ago 129cc6f052e3111. The commit mentions something about local deletes, but I don't know the code well enough to understand what the change accomplished.

Its easy of course to revert that commit, but I don't know the consequences of that on the local delete issue. Or if the change should not be reverted, then the objects need to be mapped from the private context to the main context.

Happy to fix either way, just need some guidance.

valeriomazzeo commented 9 years ago

It is probably best to see if @jeffarena remember anything about it.

jeffarena commented 9 years ago

A revert of that commit would cause breakage of local deletes due to the set comparison here:

https://github.com/RestKit/RestKit/blob/development/Code/Network/RKManagedObjectRequestOperation.m#L764

You would end up comparing localObjects (retrieved from self.privateContext) with managedObjectsInMappingRequest (retrieved from self.managedObjectContext). A proper fix would likely involve rolling back https://github.com/RestKit/RestKit/commit/129cc6f052e31116ce12502d1246f04ddd5ad85b and then refactoring of the deleteLocalObjectsMissingFromMappingResult:error: method to avoid cross-context object comparisons.

dhalman commented 9 years ago

I believe we are running into this issue as well: recent changes to a managed object are not reflected in the query results, but are reflected on the objects returned.

For example, searching for is_favorite != nil returns 2 results. Set is_favorite to nil for one of them. Query again, returns same 2 results (but one is marked as favorite and one is not when analyzing the objects returned)

dhalman commented 9 years ago

@jeffarena is there any progress on this issue? we will likely be removing rest kit from several of our projects if this is not resolved.

Please see this issue on stack overflow: http://stackoverflow.com/questions/31082661/core-data-fetch-predicate-nil-check-failing-unexpected-results

Thank you for your time.

Adlai-Holler commented 9 years ago

+1

segiddins commented 9 years ago

I haven't had the time to investigate this, but a PR with a fix would be very much appreciated :)

valeriomazzeo commented 8 years ago

@cfis could you please create a test that reproduces this issue?