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

烂漫一生 提交于 2019-12-24 13:13:36

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/24325137/restkit-comparing-local-objects-and-remote-objects-before-mapping-or-actually-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!