Make a Firebase Firestore collection available offline - android

Is there a way to make a Firestore collection available offline on Android?
I know there's setPersistenceEnabled(true), but I think it only caches data after I first access it. There's no upfront caching.
I would like to have certain collections to be always fully local stored, so that if device is offline it can still query for data that have never been queried before. (Of course it would require internet connection at some point in time so that data can be synched)
I thought on doing a get on the whole collection at some point, and then attaching a listener so that the app gets new data. Is it a good practice? Is there anything better?

Yes, you have to query the data before it's available locally. There are no other options.
It's a good practice is it meets the requirements of your app, and you're willing to pay for the cost of that query, and all the changes to the doucments that happen over time (each changed document while the listener is attached costs 1 read).

Related

Android: store data locally or not?

My Android app is fetching data from the web (node.js server).
The user create a list of items (usually 20-30 but it can be up to 60+). For each item I query the server to get information for this item. Once this info is fetched (per item), it won't change anymore but new records will be added as time go by (another server call not related to the previous one).
My question is about either storing this info locally (sqlite?) or fetching this info from the server every time the user asks for it (I remind you the amount of calls).
What should be my guidelines whether to store it locally or not other than "speed"?
You should read about the "offline first" principles.
To summarize, mobile users won't always have a stable internet connection (even no connection at all) and the use of your application should not be dependant on a fulltime internet access.
You should decide which data is elligible for offline storage.
It will mainly depend on what the user is supposed to access most often.
If your Items don't vary, you should persist them locally to act as a cache. Despite the fact that the data mayn't be really big, users will welcome it, as your app will need less Internet usage, which may lead to long waits, timeouts, etc.
You could make use of Retrofit to make the calls to the web service.
When it comes to persisting data locally within an Android application, you can store it in several ways.
First one, the easiest, is to use Shared Preferences. I wouldn't suggest you this time, as you're using some objects.
The second one is to use a raw SQLite database.
However, I'd avoid making SQL queries and give a try to ORM frameworks. In Android, you can find several, such as GreenDAO, ORMLite, and so on. This is the choice you should take. And believe me, initially you might find ORMs quite difficult to understand but, when you learn how do they work and the benefits they provide us, you'll love them.

Should i keep a local copy of remote database?

I am working on an application that will, basically, allow people to create, join and manage groups of other people. The people within the groups can also message each other.
I have been wondering which path would be better:
Keep a remote database with all the information, including messages sent to and from users. And have the app query the server every time it needs information. Even information it has seen before.
Keep a remote database with all the information, including messages sent to and from users. Also keep a local copy of the remote database and just keep it synced with the remote database. Whenever the app needs to query for information, it does a query to see if the local table is up to date. If it is not up to date, it updates the table and runs the query on the local table. This way it will keep a local copy and the app will have fast queries when there is not an update to the remote table.
What is generally done with mobile applications and remote databases?
Would it be "bad practice" if i just did number 1?
From my point of view, in most cases, the database in the mobile is just a cache of the real database, the one in the server. So, my suggestion will be to keep locally all data that you need syncing with the server. This allows you to show information even when no connection and show something to the user while the info is updated.
Also, this approach makes the local data volatile without risk, as it's stored in the server. So:
All info is in the server
With a background process (service, thread, intentservice, whatever best suits you) you sync this information with the local database
UI is always showing info from local database
Of course, this is a very general approach, and needs to be examined for each case as different situations may need different approaches.
My base response is that I would keep the data in one place and access it remotely unless there is a major reason to keep it locally. There would have to be extenuating circumstances to mandate that I keep a copy of the data locally. Just make sure your queries are accurate and concise. Don't pull over more data than you need to.However, you can have a subset of data kept locally. Items that are specific to the user (like messages), but keeping data that is not relevant just adds overhead and bloat.

Android + Couchbase usecase?

I am currently developing an application that provides some temporary static, but often changing data.
You can think of it like a telephone book. Most of the entries might never change, but there might be cases where people change their telephone number or move in/out. The dataset isnt large but has to be available at any time.
My first thought was to implement a clientside-SQLDatabase which queries a backend in certain intervals asking for updates (stored in another database on the backend server). If there was an update since the last query, the updated data will be send over and stored in the local sqldatabase.
Now I stumbled over this interesting looking database model Couchbase. Altough I dont see through it completly by now, if I got it, I can use it to keep the client-side database in sync with the servers database. Is this a usecase for couchbase or should I go with another strategy?
Thanks in advance!
That's a perfect use case for Couchbase. You'll want to check out Couchbase Lite (https://github.com/couchbase/couchbase-lite-android) and the Sync Gateway (http://docs.couchbase.com/sync-gateway/).

how to cache retrived data from online DataBase?

I have an application that reads information of songs and singers from Mysql DB and display it in listView ... my question(s)
is it a good idea to cache the data that the user already retrieved so he does not need to retrieve it every time?
how to do that chasing if it is a good idea(give me hints only)?
how to match between what is already cached and what is need to be retrieved from Mysql
It's always a good idea to cache values coming from external sources, as it reduces the time the application takes to display them.
This can be implemented in multiple ways. Generally said:
Binary data (e.g. Images) should be cached on internal/external storage. See "Caching Bitmaps"
Textual data (e.g. Articles) should be cached in memory (for example in a Map)
That said, the important and tricky part of a caching system is to determine, when a cached values is no longer "new" enough. While choosing the specific criteria depends on your application and personal taste, here are some general hints:
The age of the data (you can give server-side hints with the HTTP Response-Header Expires)
The priority of the data. The more important the data, the more up-to-date it should be.
The likelihood of any changes at all. The comments on a article are more likely to change frequently than the article itself.
A nice implementation would be to just delegate all network requests to a caching-aware method/class (implemented from the above hints), which would then decide if the request needed to be done in the first place and return either fresh or cached data.

What should be the preferred and efficient way to use database in android?

I have created a database for my application. I will use the data in this database, again and again. I just want to know the preferred way to do this.
One way is to query the database again and again, and the other way in my mind is to have multiple ArrayList for different fields, or something like that, to store data in the start of application in these Lists and then access these Lists whenever I need data(sort of a disconnected mode e.g. DataSets that we have in .Net).
So, which is the efficient way or is there any 3rd better way?
Is your database local, local and external (shipped to your apk) or remote? If it is local then you can query the database everytime you need. Because you rather have operation with your CPU than keep memory occupied. When it comes to remote database than you can implement various ideas about keeping the last query in case of disconnection, then on the next query release the last query results (not good to me, because you might have milion of results, which will occupy so much memory [scenario]), or save the query results (get result - translate into query for your own database = long process). Or you can query again the database again and again, with least effort so many results. I hope you get my idea and will serve you.

Categories

Resources