RestKit/RestKit — Issue #520: Need a way to reject server changes without interfering with MOC saves
Author: rayfix
Created: 2012-01-23 09:17:50+00:00
State: open

I am opening this issue because, as far as I can tell, the proposed pattern does not work in a system using CoreData that needs to be able to save MOCs at random times. If I am somehow mistaken please let me know.

On Jan 11, 2012, at 11:47 PM, Ray Fix wrote:

This technique is really close to working for us but not quite without forking. The reason is that the method validateValue:forKey:error: is overridden by NSManagedObject. So when we set isDirty to YES, the CoreData gods become angered and our MOCs stop saving every which way.

The "workaround" we found is to hack RestKit to use validateValue:forKeyPath:error: which is used only by NSObject. Core data stays happy but objects are prevented from mapping. The change is here:

https://github.com/rayfix/RestKit/commit/5c0929e700b83db52084e66755e8fe2f15311838

Unfortunately the spaces got messed up so the diff looks big but the only change should have been forKey: -> forKeyPath:

Is there a better way to achieve CoreData's validation requirements and prevent RestKit from mapping? How would people feel about adding higher level predicate like "preventMapping" you could call on a per-object basis to accomplish this goal?

Ray

On Aug 26, 2011, at 6:07 AM, Blake Watters wrote:

There is not framework level support for this kind of intelligent synchronization yet, but here's an approach that may work for you:

RestKit supports key-value validation. When you can do is set a dirty flag on your objects and flip it to true when you have locally modified content. You can then add a key-value validation method to your objects and reject all the changes from the server by returning NO when isDirty == YES.

See - (BOOL)validateValue:(inout id *)ioValue forKey:(NSString *)inKey error:(out NSError **)outError;

in NSKeyValueCoding.h for details. There are also unit tests the show how to use it in the Specs directory. Search for validate.

--
Blake Watters
Sent with Sparrow

On Wednesday, August 24, 2011 at 10:15 AM, gri wrote:

Forgot to mention that I'm using the CoreData mapping.