Parse Platform on iOS: best way to replace changed local values with more-recently changed server values?

58 Views Asked by At

So imagine the following scenario, using the Parse platform on iOS:

  1. I get a PFObject from the server, let's call it GlassChalice.
  2. Someone else, let's say Bill Blofeld, changes GlassChalice from a different location.
  3. Later, I make some changes to my local GlassChalice, but don't save them to the server.
  4. Still later, I want to update GlassChalice, but I want to update it to the current server values, in other word Bill Blofeld's values. I do not want to replace the server values with my local values, and also do not want to reset my local values to the values GlassChalice was loaded with.

So if I use revert(), will I get what I want?

According to the Parse docs:

- revert Clears any changes to this object made since the last call to save and sets it back to the server state.

...but, as in my example, clearing "changes made since the last call to save" and setting it "back to the server state" aren't always the same thing.

So far this seems like the only way to guarantee the results I want, but it has one obvious problem:

public func updateObjectFromServer(_ objectToUpdate: PFObject, then doThis: (()->Void)? = nil) {
    let query = PFObject.query()
    query?.whereKey("objectId", equalTo: objectToUpdate.objectId!)
    query?.getFirstObjectInBackground (block: {
        (serverObject, error) in
        if error.isNil() {
            objectToUpdate["numberOfLimbs"] = serverObject?["numberOfLimbs"]
            objectToUpdate["eyePlacement"] = serverObject?["eyePlacement"]
            objectToUpdate["crossStitchingTalentRating"] = serverObject?["crossStitchingTalentRating"]
            objectToUpdate["clamsEaten"] = serverObject?["clamsEaten"]
        } else {
            //handle error...
        }
        doThis?()
    })
}

But the huge problem here is that I have to know all the key names, and type them in explicitly, for this to work.

Is there a better, more generic, way?

0

There are 0 best solutions below