-
Notifications
You must be signed in to change notification settings - Fork 108
Automatically sync when connectivity resumes #4
Comments
I am also concerned, it is written that "By default it saves every model locally" then how will I be at peace that the changed models are always synced to server so that when user opens the app from somewhere else he gets the same state? |
You can't be at peace, at least not at the moment :) You can do a manual sync periodically, which is what I'm doing atm. |
Thanks for the straight answer, but you can just set an option like sync-interval after which the local store would automatically sync to the server or some other scheme. So that the syncing functionality is also contained :) |
That may be what I'll do. I'll look into this more closely, perhaps next week. |
Thumbs up! |
A potential solution here is to create an 'envelope' object that goes around each locally-stored model. The envelope could look something like this: To save space, use timestamps for times, one letter attribute names and 1/0 in place of true/false: To save more space, make the envelope an array, rather than an object. This saves 4 characters per array item per localStorage item over using an object with named attributes. Format: One more space-saver. Switch to seconds instead of microtime, switch from lastRemoteSync to lastRemoteSyncOffset (setting to 0 now - logic for this would still need to be implemented) and junk the isDirty item. If lastRemoteSyncOffset === 0, we're clean; if it's > 0 , we're dirty; if it's < 0, we were able to successfully sync remotely, but were unable to save to localStorage; if it's It will need some fleshing out - for instance, setting the localSave and remoteSyncOffset values dynamically. I only did these in javascript. If you'd like, I'll build some version into the CoffeeScript file, compile the .js from there and send a pull request. I actually suggest a mesh of the first and last ones - use an object for better extensibility but include all of the other trimming down I did in the other two examples. |
kurtmilam you sir are doing great :) |
@kurtmilam Pretty nice. I think I'd prefer keeping a separate record that maps between keys and such data, rather than wrapping everything. |
Also a good idea. I'll rework it this evening or tomorrow. Any specific ideas on the implementation? By the way, are you concerned at all about storage limitations with localStorage? I understand 5MB is a hard limit, and have read that Chrome stores strings encoded UTF-16, effectively halving the limit. According to that linked thread, the Chrome team isn't interested in upping the limit, either. I think it'd be smart to consider writing dualSync in such a way that it can be extensible to work with different types of local storage - HTML5 localStorage, sqlite, indexed DB, etc. |
I was thinking of something simpler, like As for separate adapters, it should be possible to simply put in a different Store class. Atm we're not worried about going over quotas since we're moving around little data. |
I created a sync this on my fork: https://github.com/thiagobc/Backbone.dualStorage/tree/sync_local_and_remote . I started with the idea to create a _dirty and _destroyed localstorage items.
thoughts? |
This is again similar to what I had in mind, but haven't had time for. Looks nice. |
For future reference http://www.html5rocks.com/en/mobile/workingoffthegrid |
Right now, a manual sync is required after connectivity is resumed. Dualstorage should keep track of unsynced models and help with syncing them later.
The text was updated successfully, but these errors were encountered: