Java Android : Architecture for background Sync and database management - android

I need to write code for an application that is like CRM (not exactly though) I need to get data from server in background and populate my tables.
And update UI layer when I have new data/when sync finishes, I need to have a background service that checks every minute (when app is running) for any update on server
There are around 25 tables that need constant sync
To start with I want to write some architecture that is memory battery and of course bandwidth effective
are there any opensource framework that I can have look,( I looked into groundy already) to ensure best architecture for my app

you can try Sync Adapter
take a look at this Creating a Sync Adapter

Related

Display real-time data in ListView

How to automatically update UI with the new tasks?
For example, while I'm on the tasks page, I load them once. In the meantime, let's say new task are created and stored from my web application to mysql db. I would still see only previously loaded tasks without any idea that the new record is created in the database.
I'm using ListView.builder to display the data, while calling the API on initState(). Also, it might be worth noting, I'm using Laravel with MySql for my backend.
What is the best way to update UI with new data whenever there is a new record in the mysql database?
i have one solution to get real time record/Tasks with out page refresh on UI.
steps
1)Add new column name (is_seen) on Task table default is 0.
2)when tasks list you will show on front end then add is_seen =1 for all that Tasks that you have to show on front end.
3)Run ajax call in interval in Task Tasks Ui page that run after some interval like 3 sec etc and get all unseen tasks (condition is is_seen=0) on Ui Tasks page.
4)when new task create on table that will have is_seen=0.ajax call get that task and show on Tasks list on front end
5)then same time after rendering on new task on task list send ajax call to set is_seen=1 of that task.
I hope you understand this.
This might need a little bit of a more complex setup, as you are trying to build an app that is sort real time.
You could go explore Polling -> where you make call to the api endpoint after an interval of say 30 seconds
Or better yet you could explore websockets.
I would argue websockets is the better alternative.
Polling doesnt require any change on your backend, just a timer countdown of sorts -> Have a look at this : https://api.flutter.dev/flutter/dart-async/Timer/Timer.periodic.html.
For websockets, Pusher ,an abstraction of the low level websocket implementation, is quite common on Laravel and quite straight forward to setup. You could start with with the with this closed source option https://pusher.com. Its easier very well documented and has pretty well maintained Flutter package.
On your flutter app, I would advice a better form of state management like Bloc or RiverPod coupled with the Pusher flutter package (https://github.com/pusher/pusher-channels-flutter), to subscribe and listen to channels and events. The state management bit is just to enable you to work with streams and to have cleaner and maintable code.
Depending on how you architecture of streams you might also need to explore the Streambuilder widget

Syncing mehod for android chat application

I am implementing a chat app in android. A vital part of this app is to sync with the server and local database. There are several methods to sync data between server and android device like AsyncTask, IntentService and SyncAdapter.
I prefer to use SyncAdapter, because it is more efficient and it handles most of the background tasks by itself.
When I read the developer page for SyncAdapter I found this,
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.
Does that means is it not good to use like chat app?
Also I need to mention a feature of SyncAdapter
Automated execution
Allows you to automate data transfer based on a variety of criteria, including data changes, elapsed time, or time of day. In addition, the system adds transfers that are unable to run to a queue, and runs them when possible.
So if it starts to sync when data changes (Since the new messages are stored in the sqlite database), I think SyncAdapter will be a good choice for Chat App.
Any Suggestions are appreciated.
Thanks.
Usually mobile app depends on backend implementation and app requirements, but generally you shouldn't use such methods for chat application, they won't give you up to date data.
I'd say when app is in background, you should use GCM for new messages notifications and when app is in foreground use something like RPC, xmpp, sockets or whatever that keeps your connection alive.

Is there a simple way to sync android app between devices?

I have a list I keep in SQL dB and would like to sync it with at least 2 android devices.
I tried reading into sync adapter and it seems very complicated and too much for syncing a list.
Is this my only option or there is a simpler option?
The problem is I am short in time. So I wouldn't like to start implementing a general and complicated solution when I can implement something more specific and simple. This app is for myself only so I'd like to keep it simple.
If you just want to sync a list you can create a web service (server side). Identify the business logic e.g. say you want to keep the union of two lists.
You can have sync implemented from server side using push notification or may be say once a day when the app comes in foreground. You can also have a alarm based syncing where you can schedule this process.
Then you may not need whole adapter stuff. You can just consume this service and sync your data. Just decide for yourself.

SyncAdapter alternatives

I think its well known that in list of worst-documented topics, SyncAdapter shines bright like a diamond !
acording to http://udinic.wordpress.com/2013/07/24/write-your-own-android-sync-adapter/ SyncAdapter brings 4 main benefits :
A) Battery efficiency
B) Interface C) Content awareness D) Retry mechanism;
if in any case there's a need to sync an sqlite DB with remote SQL DB, and none of these benefits is needed, what other alternatives are there**?** its easy to manage a service in-between the DBs with php, I did that for Uploading part of syncing process,but for the downloading part I feel silly if I use the query filling method,cause in near future remote db might get large and larger.the only solution that comes to my mind is to write my own sync activity/service, but I dont know how to access the last update date to SQLite db/table (other than specifying a _date in every table,) to check if it is necessary to sync again ? I feel my head is between two places!
You are mixing the problem.
1- Do you really have to use sync Adapter ??? So if yes, you are gonna have a Sync call per table and no needs to save the last call date. Android will do it for you. Just setup your sync timers properly
2- other solution is to do a simple AsyncTask and do your job here. (For exemple if you have to do it only once per week)
For your date problem, the thing is if you really wants to know if you are up to date you got many solutions. On your server save the date, or increment a version and compare these when you call a sync from your device to know if you have to sync or not.
An other solution is to simply just refresh your db wherever it is updated or not(for exemple you got a small db, so no need to create an optimized system).
I faced the same problem months ago and hoped this helped you.
You might want to consider this article:
https://www.bignerdranch.com/blog/choosing-the-right-background-scheduler-in-android/
It makes it clear how syncadapter is a good choice as a result of lesser convenient options when needing to utilize the battery well and go out to the network.
I don't recommend Asyntask for theses reasons:
http://blog.danlew.net/2014/06/21/the-hidden-pitfalls-of-asynctask/
If syncadapter is really not working for you there is
android's best practices which suggests to use an IntentService and WakefulBroadcastReceiver with partial wake lock when doing long-running operations. It says "the Android framework offers several classes that help you off-load operations onto a separate thread that runs in the background. The most useful of these is IntentService."
https://developer.android.com/training/run-background-service/index.html
https://developer.android.com/training/scheduling/wakelock.html
there must be some truth to it since they wrote it.
Android Jetpack includes WorkManager which is a valid alternative to syncadapters.
Main features:
Schedule a job according to network availablity or device charging status
Backward compatiblity up to api 14
Ensures task execution, even if the app or device restarts
Intended for deferrable tasks (E.g periodically syncing application data with a server)
In alternative, something similar is Android-Job library by Evernote

Why to use sqlite Database in Android?

Why do we use the sqlite data base in android.I am developing an android application where the data is to be fetched from the server and do some data calculation and show it on the UI.
Is it good for me to fetch the data into the sqlite DB and update the UI on regular interval from the sqlite in evry 20 minutes or will it be good to sent the Http get request to the server and update the data from teh response on the UI.
I wanted to know which one will be better and why?Why to involve sqlite DB?
The data corresponds to some 40X40 table data on which some heavy mathematical processing is to be done and then displayed on the UI(similar to Stock market application) and data needs to be cleared after every 12 hours.
plz advice
Rgds,
Raul
It is good to use database in your case.
Pros:
If your application gets closed the in memory data will be lost, but after that you will be able to restore the state from the database if you have one
Especially for the case of complex calculations it is good to store the result once in the database and not recalculate it multiple times on demand
The database will untie your UI from the internet connection and thus you will be able to display results even if there is not internet connection
Using database you will be able to fetch the updated data from a background service, without impacting your UI
Organizing your data in database usually makes it a lot easier to manage all the application data.
Cons:
Adding database will require a bit of additional effort on your side
As you can see my list proves you SHOULD use database in your case. Maybe I am biased, but at least I provide you with things to consider.
It's really a design decision, SQLite offers a very robust way to organize and persist your data, you're only other options are to write to a file, or to save in SharedPrefs, both methods become a lot harder to manage once the size of your data begins to grow, as you must manually keep a list of objects and manage their names etc etc. 40 x 40 table data is large enough to justify using SQLite, even if you are dropping and recreating the table every 12 hours.
You might want to consider using an ORM library to make fetching and saving data from the DB simpler, ORMLite is good and compatible with Android
http://ormlite.com/
If your application relies heavily on an internet connection you don't need to buffer information in the database. However if you want to use the app where you have bad or no signal you might want to used cached values from the sqlite database.
With slow internet connection your application may be unresponsive so caching may be a good idea - but it doesn't necessarily be in the sqlite database. You should use the sqlite database for data that is required by the device frequently and that is irrelevant to your server component.
If the data is updated frequently but only while the application runs you might want to cache in the devices memory. I assume your app is not running all the time within the 12 hours but is called regularly instead to check something.
12hrs is a long time, so rather than leaving your data wander in RAM, i would suggest you to use database. Because you never know when you may need to read it again.
Otherwise, if your purpose is only to downloaded data, process it and display in activity, then you dont need to involve database because if your app is closed (due to user or low memory), in anyway your app will be downloading fresh data from server... am i right?
> update the UI on regular interval from the sqlite in evry 20 minutes
Dont expect your app to be open for such a long duration.
To precisely suggest to your case
Avoid DB
Fetch Data at app start or at appropriate time when app is opened
and save it in plain java objects.
Define Methods within it that perform operation in it.
Make map or list to save those POJO
Define Seprate Controller Classes within your project to update map of pojo at any
specific change to make fresh data available to UI.

Categories

Resources