satheeshwaran satheeshwaran - 4 months ago 15
iOS Question

RestKit comparing local objects and remote objects before mapping or actually inserting into CoreData DB

So I have started off with implementing my second project in RestKit, the previous one was very simple, just pull stuff from a remote service and fill in my core data db. In the current application I am working on a sync functionality, so I want to validate stuff before doing an insert i.e, compare an object in the local store with the object that I get from the remote service.

I was reading this thread and found out that I could achieve something like this. So what I added these two methods to my model. I know just one method is enough but the problem is with both the functions.

@implementation News

@dynamic title;
@dynamic desc;
@dynamic time;
@dynamic category;
@dynamic content;

- (BOOL)validateValue:(inout __autoreleasing id *)ioValue forKeyPath:(NSString *)inKeyPath error:(out NSError *__autoreleasing *)outError{
return NO;
}

- (BOOL)validateTitle:(id *)ioValue error:(NSError **)outError{
return NO;
}


The entity mapping code,

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass(theClass) inManagedObjectStore:objectManager.managedObjectStore];

userMapping.performsKeyValueValidation = YES;
userMapping.discardsInvalidObjectsOnInsert = YES;


So what I thought was this method is fired whenever a object is being mapped or is being overwritten. So self would contain the current object and the *ioValue would contain the new value that would be inserted or not, is my conclusion right about this??

Now when the method is fired in my app self.title and the *ioValue contain the same value that is the new value from the service and self.title does not contain the old value in the local store(may be the changes to the db have already taken place :( ). Also if I return No from this method the record still gets mapped, which should not happen right?

Any insight about this problem would be of lot of help. I am in the verge of starting again from scratch without using RestKit.

Answer

If you always return NO from the validation methods then you should always end up with empty objects after mapping. If that isn't true then you have a basic KVC validation issue to investigate.

If you are using a plain object mapping then empty objects are fine and will always be returned to you.

If you're using an entity mapping then you can set some attributes as mandatory (not optional) and then configure discardsInvalidObjectsOnInsert on the mapping so that when you don't update some values any invalid objects will be discarded (rather than saved).

So what I thought was this method is fired whenever an object is being mapped or is being overwritten. So self would contain the current object and the *ioValue would contain the new value that would be inserted or not

Correct. Though this only really works for entity mapping with unique identifiers because that allows RestKit to find the original values.

Comments