问题
GOAL: 1) Enable users to play my game regardless of poor connectivity, and 2) have ~reliable user state stored on Parse for customer support and stats.
My Approach: I am using local client storage as the master (so that net connectivity is not required), and I am using Parse as a secondary synced storage so that I can address customer issues and maintain stats.
- The game has a sequence of fixed levels.
- User state for each level is stored locally in UserDefaults as the SOT (yes, a bit ugly).
- I use Parse as a secondary replicated store for customer support issues and stats.
- Thus, I also store the level state on Parse (i.e. PFObject="UserLevel": userId, level #, status, high score, #wins, #losses, etc.).
- There should be only one (or zero) PFObject per level per user.
==> When the network connectivity is poor, I often create multiple PFObjects for the same level.
e.g. A typical 5 minute game session:
User unlocks a level:
==> Create PFObject "UserLevel": uid=currentUser, level=#, status=UNLOCKED, wins=0, etc.User plays this level and loses:
==> Query (async) Parse for PFObject (match userId & level #):
If one matching object found ==> ++losses... (saveEventually)
If >1 matching objects found ==> object[0]:++losses... (saveEventually) (ignore all but one!)
Else (no matching objects found) ==> Create new PFObject. (saveEventually)User plays again and wins:
==> Query (async) Parse for PFObject (match userId & level #)
If one matching object found: status=COMPLETED, ++wins... (saveEventually)
If >1 matching objects found, status=COMPLETED, ++wins... (saveEventually) (ignore all but one!)
Else (no matching objects found): create the PFObject. (saveEventually)... etc...
As you can guess, if the network connectivity is slow, and the various queries and saveEventually's have not completed yet, this can easily result in duplicate PFObjects for the same level.
My guts tell me not to create the PFObject during an update if it does not exist. Instead, the Parse state these be sloppy/behind, and clean it up during app start (i.e. a clean sync).
I'm assuming this is a very common design pattern and that I'm missing some basic CS fundamentals. (I'm a newb to backend coding.)
回答1:
The simple solution, since there are a fixed number of levels, is to pre-create each "UserLevel" and save them, and then everywhere else you just do updated instead of create/update.
来源:https://stackoverflow.com/questions/25126862/syncing-local-state-and-remote-state-parse-with-poor-connectivity