Persisting and keeping mobile app data in snych with online backend

404 Views Asked by At

I am building a mobile app using AngularJS and PhoneGap. The app allows the user to access a large amount of data-items. These data-items come with the app in form of a number of .json files.

One use-case is that a user can favorite any of those data-items.

  • Currently, I store the (ids of) the items that have been favorited in localStorage. It works and it's great and very simple.

  • But now I would like create an online-backend for the app. By this I mean that the (ids of) the items that have been favorited should also be stored on a server somewhere in some form of database.

Now my question is:

  • How do I best do this?
  • How do I keep the localStorage data and online-backend data in synch?

  • In particular, the user might not have an internet connection at the time were he favorites a data-item. Additionally, if the user favorites x data-items in a row, I would need to make x update calls to the server db, which clearly isn't great.

So, how do people do it? Does Angular have anything build-in for this? Is there any plugin? Any other framework?

This very much seems like a common problem that must have a well-known solution?

1

There are 1 best solutions below

1
On

I think you've almost got the entire solution. All you need to do is periodically (on app start load the data from the service if available, otherwise use current local storage, then maybe with a timer and on app close update the data if connected) send the JSON out to a service (I generally prefer PHP, but Python, Java, Ruby, Perl, whatever floats your boat) that puts it in a database. If you're concerned with merging synchronization changes you'll need to use timestamps in the data in local storage and in the database to make the right call on what should be inserted vs what should be updated.

I don't think there's a one size fits all solution to the problem, though I imagine someone may have crafted a library that handles the different potential scenarios the configuration may be as complicated as just writing the logic yourself.