0

Using restkit, I'm POSTing to a server and getting back objects. One of the properties on these objects is a timestamp when the object was received.

My current implementation is to loop through the objects in the mapping result array of the RKObjectManager's postObject success block and if the object doesn't have the received timestamp, I add it and save the object, doing this:

        NSError *error;
        if (![object.managedObjectContext saveToPersistentStore:&error]) {
            // handle the error here
        }

I have an inkling there's a better way to do this, and I actually think this might be causing some other issues but I'm not sure. Can I simply call save instead of saveToPersistentStore? Will doing the following be better or worse for any reason?

        NSError *error;
        if (![object.managedObjectContext save:&error]) {
            // handle the error here
        }

Also, if there are numerous objects in this mapping array will calling this in a loop n times be problematic?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Mike
  • 9,765
  • 5
  • 34
  • 59
  • Basically you always want to save to the persistent store. Not doing so will lead to (human) errors when you forget what is and isn't persisted. Saving on each iteration of a loop should be avoided, better to save in batches. – Wain Jul 07 '14 at 15:36
  • so rather than looping through and saving each, after I'm done looping through and setting each property on each object I should call [context saveToPersistentStore:&error]? – Mike Jul 07 '14 at 15:38
  • Preferably you should do it so you don't need to save the context. RestKit saves the context so if you update during the mapping process then all of the work is done on the background thread and everything is cleaner. – Wain Jul 07 '14 at 15:39
  • How would I go about altering the mapping process? I know on managedObjectRequestOperation you could alter it, but now that I've changed to postObject instead it seems that is not available. – Mike Jul 07 '14 at 15:43
  • Oh, I did follow the link provided above regarding the duplicate post and then a comment made in response to one of your comments, and it looks like I can add a willSave to my object and do it there, that might be cleaner. – Mike Jul 07 '14 at 15:48
  • Check the linked question in the comments too, it provides valuable additional info. Note that you could also get the server to send the timestamp and it could potentially make life easier in the future... – Wain Jul 07 '14 at 15:49

0 Answers0