Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34180

Offline database capability while using firebase

I'm new to Firebase, and I was researching about it to see if it fits our needs. It has everything we need except an offline database. Well, I know that it has the capability to cache changes when the user is offline and then sync them when the user becomes online, but this is not what I'm talking about.

As firebase is costly, we want our free users to be able to use the app only offline and the data should not sync to the cloud no matter the user is online or not, and only use sync for subscribed users.

One solution which we have not yet put much thought into is to use an offline DB like SQLite and:

a) when the user subscribes move the data to firebase

b) if the user cancels the subscription move the data to SQLite

but this solution needs 2 completely different codings the same thing. Extra code for migrating from SQLite to firebase and from firebase to SQLite. Is there a better solution to use the Firestore database and also have a complete offline database functionality?

Upvotes: 4

Views: 84

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 1

In my opinion, your solution can work. But there are some situations that you should take into consideration.

a) when the user subscribes move the data to Firebase.

As I understand, it's one or the other. In this case, when a user gets subscribed, you should always consider locking the local SQLite database for writes, until all the data is written on the Firebase servers. When the operation completes, only then you should allow the user to write data in the cloud.

Why is this needed? To always have consistent data.

b) if the user cancels the subscription move the data to SQLite.

If the user cancels the subscription, you might consider using almost the same mechanism as above. But first, you have to clear the local database, as it will contain outdated data, and then copy all data from Firestore right into the SQLite database.

Edit: You can also take into consideration copying the data from the local cache rather than getting it from the Firebase servers. This will imply no costs.

but this solution needs 2 completely different codings the same thing.

That's correct, but since both, the offline and online databases share the same fields, the operation might not be so complicated as think. Simply attach a real-time listener on a property within the user object, most likely called "subscribed" which can hold the value of true/false, and take actions accordingly when is changed.

Upvotes: 1

Related Questions