leaguevine / leaguevine-ultistats

MIT License
18 stars 4 forks source link

WebSQL - Persist syncing queue #73

Open cboulay opened 12 years ago

cboulay commented 12 years ago

Entering data then clearing the cache before the data are sync'd will result in the data being persisted locally but not remotely, because the queue to sync is in the cache only. An obvious solution to this is to persist the queue in its own localStorage or WebSQL db but that leads to two problems, the first of which I just found the solution to on SO: -http://stackoverflow.com/questions/1366127/instantiate-a-javascript-object-using-a-string-to-define-the-class-name -If the queue is persisted locally then objects that are instantiated from the queue are separate from objects that are living in the app thus sync'ing an object created from the queue will not trigger events bound to the twin object living in the app. To do this we might need to identify objects in memory to see if they match the object that is about to be sync'd and if so use the in-memory object instead. Do you have any idea how to get a list of objects in memory?

mliu7 commented 12 years ago

This is a challenging problem. The best solution I've seen for this is to force the app to synchronize any data (i.e. flush the rest of the queue) before the app clears the cache and loads the new version. This is the approach taken by this backend and I think it makes a lot of sense:

https://github.com/lucian1900/Backbone.dualStorage

Forcing the queue to be completely emptied before the refresh really seems like a simple and clean solution to me.

cboulay commented 12 years ago

Thank you for finding that repo. It has a lot of useful tricks. I wish I had found it a couple months ago.

Our app doesn't actually reload unless the user manually clicks on refresh or enters a new url. All of the hrefs are intercepted and processed through the router, thus the cache remains. I'm more worried about accidental cache clears caused from browser crashes.

The cache can also clear if the user manually clears the cache but I expect they would also end up clearing the app data including the database, so there's nothing we can do to store the data in that event.

mliu7 commented 12 years ago

Ok, cool. I wouldn't worry about accidental cache clears from browser or OS crashes. There's really not much you can do about that...

And as you said, there's also nothing you can do if the user manually clears the cache.

cboulay commented 12 years ago

I'm worried about it in the context that someone collects 4 games worth of data in an area with no coverage then their battery dies. All of that data will be lost... I think. It's rare, but not unfathomable, and it's the kind of devastating situation that would prevent someone from using the app again.

mliu7 commented 12 years ago

If their battery dies, I'm pretty sure that data wouldn't be lost. Your cache isn't cleared just because you restart your phone.