I'm a new Android developer and could use some advice on a problem I've come across.
I have an app which is querying data from the Amazon Product API on almost every activity you open. I'm able to get the data, but the activities take forever to load because I'm constantly running API queries.
The app is basically a video game review app. It wouldn't be uncommon for users to load the same data for a game multiple times. I'm thinking that making api calls for the same data over and over again is very inefficient.
My question is in this scenario, should I be saving game data to a local, or even remote (Firebase) database every time data is retrieved from the Amazon API? And then whenever data needs to be retrieved, I check first to see if it's present in the database before making the API call?
If this is correct, where should I be saving the data (shared prefs, SQLlite, internal storage, etc.)? If not, what can I do to make the app pull & display data faster?
When you say that activities take forever to load, I assume that you are not making these calls from within the Activities. And lets answer your questions one by one.
1) Shared Prefs are not a good option. (Try to use Shared prefs only when you have a bunch of things to persist). While in this case if you want to save effort of writing helper classes for database you could serialize the data (Internal Storage). And its preferable to store data locally in my opinion.
2) Do not always request the data.Only update it when the user wants the data to be refreshed (hustle free, user presses a refresh button). Or you could update data after fixed time intervals. This can involve upgrading reviews in background even when the application is not running by starting Service using Alarm Manager. You will have to read about it though.
Related
I am using axios for API calls, and Async storage for storing some details like user profile and all. But Async storage seems very slow.
My apps' key functionality needs internet to work, but lot of screens would have content which doesn't update in realtime or always. How do I save all that info in local so whenever new session of the app is launched, the screens show fully loaded instead of querying data from API and showing a loader, the info update can happen in the background. Like how all the major apps work is by not loading already loaded content but somehow saving it on the device itself, but at the same time allowing for updating if new info comes in.
Some sort of DB in the app to store all info would work? if yes, what's the best to go with Redux, Axios implementation that is fast and not as slow as Async Storage?
Async storage should work fine. To implement this, you will need to make sure to use the API data only on the first app start, and not subsequent starts. Then add an updater function that checks for updates. Here's a rough implementation.
When App loads, try to get your data from the local storage. If it's the first start, there's nothing in the storage, so fetch the data from API.
Once data comes back. Save it to the local storage for next app start. Display the data.
If the data exists in local storage (subsequent starts), you will need to send a API call to fetch the latest data. Once the data comes back, put it in the local storage. Make sure this is not a blocking call. It's up to you if you want to update the current data on screen.
I have worked on several projects using the redux-persist library. In short, what it does is cache the information you handle with redux, without having to worry about saving information with Async storage.
If you really don't want to use Async storage and you already use Redux, I think it's the perfect option. I have been with this library for about 2 years and so far I have not had any problems.
For more information visit:
https://github.com/rt2zz/redux-persist
This is just a information question. I'm new to Android app development and currently I'm working on my first app and and it is ready for the release. Now I'm concerned about how to handle heap of users and where to save all their details my app is a service booking app so it needs to save all the order details products details and lots other stuff.
Currently I'm using cloud firestore to load and save all the data of app. But I'm having some issues like without authentication it won't allow users to access some of my data and other. I wonder how large apps save their data and load them perfectly.
I wish someone will help me how can I save all my app data and load them perfectly in app. And suggest me for a best way to manage large user base. And other stuff.
First of all, firestore is good option if you don't have complex backend logic on the database. For simple CRUD operations on data firestore is a good choice but as you said you have a bulk of data then you must go for the Backend database and then connect your database with Rest API. So that all your complex queries will be done on the backend and you can simply consume your API in the app.
If you have lots of data from different users, maybe you should use a central server(DB), something like Postgres or MySQL should work fine.
At the same time, you can also do some sort of caching to accelerate the fetching process, like create a small database locally(you can use Room) to store some user specific data.
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.
I need a suggestion on a design decision.
I am creating a ecommmerce app and have a lot of items (>10000) to show in my app. Now here are the 2 options I have
1) Get all the items information from the server and save in local db and synchronize the information every time (let say 15 min)
2) Get the information every time from the web server (through rest api).
There are pros and cons of both the methods. Using local db I can get fast results and less server bandwidth but will have to handle synchronization
With second approach, I will have a lot of server request to and free and load on server.
I would also like to know how does other apps like amazon and flipkart handle this. Do they save in local db or request server every time.
What you should be looking for is a mixed design between local and remote.
In terms of data types there are two major types:
blobs 'binary large objects' for example: images, videos ...
and small data (usually json/xml representation of the items).
Amazon and other web apps provide fresh data every time the app loads, and at the same time keeps a local copy of the data incase the app went offline, or sometimes even use that data in the next load while waiting for the backend.
On the other end those app maintain a cache level for large data so that they don't have to load it more than once.
But the key for this to work is to have a very fast backend that contains many features to improve its speed including:
cloud front end that allows users to communicate with the closest server around them.
memcached or any other caching technology that will keep the info about the items in the RAM in the servers and not having to query the database to get them.
what usually happens is that the backend ensures that its data always loaded in the ram/cache by ether querying the database every specific time or by pushing to cache every time an insert/delete/update happened to the database.
Here is how Twitter is doing it
One last note Your app shouldn't take longer to interact with more than a web page, its not acceptable for native apps to take longer than web apps to allow the user to interact with them.
Only load what you want to show, but cache intelligently.
For example; an image for a product isn't going to change very often, but the amount of items available for a very popular item can change every second. Decide on a case by case basis what piece of information to refresh and when.
You definitely don't want to pull down everything from your server every time someone launches the app. That does not result in lower bandwidth. It will melt your server, eat up their data plan, and fill their phone storage with products that they will never see.
I'm building a listview and getting it's data from Parse.com. At the moment, every time the app loads up it queries for new data from Parse.com, causing the whole listview to load.
I'd like a situation where the listview references a local datasource and only go to Parse.com if new data is available. Somewhat similar to what the instagram app does whereby when you load it up, the list view is already populated and would get updated if needed.
I have tried ParseQuery Cache policies but the behavior still stays the same. What would be the most efficient way of implementing this feature?
Thanks in advance.
Sync Adapters can help you with your your problem. It is generally used for account and cloud synchronization. But there is no limitation using it.
http://developer.android.com/training/sync-adapters/index.html
Synchronizing data between an Android device and web servers can make
your application significantly more useful and compelling for your
users. For example, transferring data to a web server makes a useful
backup, and transferring data from a server makes it available to the
user even when the device is offline. In some cases, users may find it
easier to enter and edit their data in a web interface and then have
that data available on their device, or they may want to collect data
over time and then upload it to a central storage area.
Although you can design your own system for doing data transfers in
your app, you should consider using Android's sync adapter framework.
This framework helps manage and automate data transfers, and
coordinates synchronization operations across different apps. When you
use this framework, you can take advantage of several features that
aren't available to data transfer schemes you design yourself
You can access sample project here: http://developer.android.com/shareables/training/BasicSyncAdapter.zip
Note: Sync adapters run asynchronously, so you should use them with the
expectation that they transfer data regularly and efficiently, but not
instantaneously. If you need to do real-time data transfer, you should
do it in an AsyncTask or an IntentService.