0

Does firebase have a native way to check if the app has the latest data on the second load?

Here is the app workflow.

  1. When the app loads I get data from firebase and display it to the user.
  2. Data is also stored locally to be displayed later.

  3. The user closes the app and the reopens it sometime in the future.

How do I check if Firebase had new data and get it only if it has?

What I am doing is that I am storing a TimeStamp on my device and on the database. On each app load I check if the TimeStamp matches.

If it does then I display old local data otherwise make call to get latest.

Is this the correct way?

Basically what I am asking is I want to display locally stored data, but if firebase has new data then I want to get it and display that.

krv
  • 2,830
  • 7
  • 40
  • 79
  • why can it not display both old and new? – Peter Haddad Feb 19 '18 at 14:13
  • Old data is outdated and not needed. – krv Feb 19 '18 at 14:14
  • if it is not needed then it is better to remove it from the database – Peter Haddad Feb 19 '18 at 14:16
  • I am talking on the client and not on the firebase server. I get data and store it locally. Then I want to display local data if no new data is present. – krv Feb 19 '18 at 14:18
  • 1
    Instead of describing what your code does, share the [minimal code that reproduces the situation that you're asking about](http://stackoverflow.com/help/mcve). But in general, there is no flag that says "give me only data that is new to me" since the database has no knowledge about what the client already has until it connects. – Frank van Puffelen Feb 19 '18 at 15:36
  • 1
    Also see https://stackoverflow.com/questions/18270995/how-to-retrieve-only-new-data, https://stackoverflow.com/questions/43440908/firebase-child-added-for-new-items-only – Frank van Puffelen Feb 19 '18 at 15:37
  • The bigger question here is; why are you doing this? The design circumvents the live nature of Firebase and could lead to stale data or data that is not always kept fresh. Wouldn't it just be easier that, instead of keeping two copies of your data, to just letting Firebase do the heavy lifting and always keeping the data fresh? It will know when to refresh data and will supply updated data whenever something is added, changed or removed. I would think a single source would be much easier to maintain and provide the user a more consistent experience. – Jay Feb 19 '18 at 17:32
  • @Jay the aim here is to 1) Create an offline experience 2) prevent unnecessary firebase calls if there is no new data. – krv Feb 20 '18 at 03:17
  • If you are going for an offline first experience, Firebase may not be the best solution; it's strengths are online, live data with the ability to offer persistence when the database is *temporarily* offline. Imagine a 'find friends' app that shows where friends are on a map and then you drive through a tunnel. The app remains responsive while in the tunnel through cached data but it's offline for only a short time and the re-sychs when the internet returns. You may want to leverage a different database solution; MySQL or Realm. Or even something like SQLite or CouchBase. – Jay Feb 20 '18 at 13:06

0 Answers0