iCloud KVS confusion

Huy guys,

So I’m a bit lost with iCloud syncing. 

Here’s the scenario:

User logs in on device A with internet access. Plays game for a while

iCloud:set("level",3);

User logs onto device B with no internet access. Plays game for a bit

iCloud:get("level"); // returns nil; iCloud:set("level",1); // Later, when the device connects to the internet iCloud:set("level",2);

User now goes back to device A

iCloud:get("level"); //returns 2

I’m trying to work out how to determine if you can detect if the user has retrieved iCloud data from the server.

I used iCloud.table(), and if it returns zero keys, don’t write to iCloud. However that’s flawed as a new user that has internet connection would also return zero keys.

Very confused. Help would be appreciated! 

Thanks,

Anyone got any ideas?

I would recommend that when you are ready to save, in particular if you know you just got an Internet connection, get the value and see if it’s a higher/newer value and only set it if its a better value. This may need you to save more date/time information but I would always assume that your local values are invalid and that your server has more updated values.

Rob

Thanks Rob,

What I ended up doing was saving variables like this:

iCloud.set("level2",1); // Level two is completed

I can then see which is completed and it doesn’t matter if it has been “overwritten”.

Anyone got any ideas?

I would recommend that when you are ready to save, in particular if you know you just got an Internet connection, get the value and see if it’s a higher/newer value and only set it if its a better value. This may need you to save more date/time information but I would always assume that your local values are invalid and that your server has more updated values.

Rob

Thanks Rob,

What I ended up doing was saving variables like this:

iCloud.set("level2",1); // Level two is completed

I can then see which is completed and it doesn’t matter if it has been “overwritten”.