[android]how to keep connection with database? - android

I want to keep connection with database.
android side: It will know if there are any data is change on database.
I want the android app to show the newest data.
No need to do anything(eg:button on click).
Do I need to use socket service? or other?
any suggestion?
Which is easy to learn for a new.
I have a idea, use HttpClient to get json from php & database in every 1s.
Is it suitable?

Trying to get data every 1 second (if that is what you meant) is asking for trouble. Mobile communications are not that reliable, if your customer goes through an area of poor reception, then your first request would not have timed out, before you have made 30 others.
I would suggest using AlarmManager http://developer.android.com/reference/android/app/AlarmManager.html
or an Android Service http://developer.android.com/reference/android/app/Service.html
polling the database no more than every 2 minutes. You could back this up with a manual "Sync" button that the user can click if the have a good data connection and are very impatient.

Related

What is the best approach to upload 1000+ records to a server that also contains images for each record from an iOS/Android app?

I have a app working offline. It is assumed that 1000+ records are created with images in each record during this period and whenever connectivity is established. What should be the approach to send all the 1000+ records to server that also handles any interruption between the network calls or API failure response.
I assume I have to send records in batches but how to handle the interruption and maintain consistency and prevent any kind of data loss.
I guess the best way here is to send each record separetely (if they are not related to each other).
If you have media attachments, sending of each record will take 2 seconds in average, if you uploading via mobile internet with speed ~2 MB/s. If you will send the large batch of records via each request, you must have stable connection for a long period.
You can send each record as multipart request, where parts are record's body and media attachments.
Also you have no need to check for internet connection, or use receiver for catching changes of connection state. You can simply use this libraries for triggering sync requests:
JobScheduler
Firebase JobDispatcher
Evernote android-job
I would suggest to use Firebase database API.
It has got nice offline/online/sync implementations.
https://firebase.google.com/docs/database/
And it is possible to read/write the data using Admin SDK for your NodeJS server:
https://firebase.google.com/docs/admin/setup
You can use divide and conquer approach means divide the task into small task and upload the data to the server.
1. take a boolean flag "isFinishData" starting with false.
2. starting upload the data on server from 0 to 100 records.
3. next record send from 100 to 200.
4. this process run until last record (1000) is not send .
5. in last record update set boolean variable true and exit from loop .
this logic would be work fine in IOS/android both.
Save your records in local Db and use ORMs for it. Use Retrofit which provide onSuccess and onFailure method for Webservice calling. To send data to server at regular interval you can use sync adapter.
1st I need to know how did you save image in local db ?
You need to create a service to catch connection status. Each time when connection is established, you submit your record as Multipart kind. You can you Retrofit/Asynctask.
Just submit 1 record per one Retrofit/Asynctask, it makes you ez to handle success/fail of each record.
You can run a single or multi retrofit/asynctask to submit one or more record, it's up to you.
If ur data has image, on server side, you have to handle process from ur server to 3rd server ( server to save image ).
This is a very broad question and it relates to Architecture, UI Experience, limitations, etc.
It seems to be a synchronization pattern where the user can interact with the data locally and offline but at some point, you'd need to synchronize the local data with server-side and vice-versa.
I believe the best place to start is with a background service (Android, not sure if there's a similar approach on iOS). Essentially, regardless of whether the Android app is running or not, the service must handle all the synchronization, interruption, and failure in the background.
If it's a local db, then you'd need to manage opening and closing the database appropriately and I'd suggest using a field to mark any sync'd records so if some records did fail, you can retry them at another point.
Also, you can convert the records to json array, then do a post request.
As for uploading images, definitely needs to be in batch if there's a lot of them but also making sure to keep track of which ones are uploaded and which ones aren't.
The one problem that you will run into if you're supporting synchronization from different devices and platforms, is you'll have conflicting data being synchronized against the backend. You'll need to handle this case otherwise, it could be very messy and most likely cause a lot of weird issues.
Hope this helps on a high level :)
To take on simple approach ,have 1 flag in your data objects [NSManagedObject] classes as sync.While creating new object / modifying an existing object change sync flag to false .
Filter data objects with sync value as false.
let unsyncedFilter = NSPredicate(format: "sync = %#", #(false))
Now you will have an array of objects which you want to sync with server.If you are sending objects one by one in requests.
On success change sync flag to true else whenever your function gets executed again on app launch/reachability status update, it will filter out unsynced data again & start synch.
As others have mentioned this is a rather broad question. A lot depends on both the architecture of the server that will receive the data as well as the architecture of the app.
If you have any control over the implementation of your backend I would recommend implementing a storage solution that allows for pausing and resuming of transfers. Both Google Cloud Storage and Amazon S3 offer a similar functionality.
The idea behind this approach is to be able to pick up the upload from where it stopped. In case of app crash or issues with internet connection you don't have to restart all from the beginning.
In your case I would still start separate uploads for each one of the records and store their upload progress.
Here you can find an example of how to use the pause / resume approach using the mobile SDK with Amazon https://aws.amazon.com/blogs/mobile/pause-and-resume-amazon-s3-transfers-using-the-aws-mobile-sdk-for-android/.
Editing adding reference to Amazon iOS SDK , http://docs.aws.amazon.com/mobile/sdkforios/developerguide/s3transfermanager.html
Best way is to break the files into chunks of 100s and upload at intervals or when app is idle.

Keep Android local DB (Realm) in sync with SQL Server with minimal network overhead

The Situation
I've been banging my head on this one for awhile. I have a local DB using Realm with a back end SQL Server DB. There are a few areas in my app where data can be changed very quickly (a checkbox for instance). The user has the ability to toggle data back and forth many times a second. So lets say the user in the matter of a second makes 10 changes. What I am trying to avoid is POSTing out to SQL Server 10 times in that second. Instead what I'd like to happen is have some sort of batch process that executes every 5~10 seconds to sync up the data thus reducing network calls.
The Caveat
How can I ensure the sync process will fire even if the user kills the app before the 5~10 second mark or while the POST call is being executed.
Call a service in the onPause() of the current activity to run after the fact? Is this
guaranteed?
Cache the data and try again on the next app launch?
Use GCM or some form of push service to initiate a scheduled sync?
I understand there is not a silver bullet for this but if anyone has ran into a similar concern any suggestions/best practices are appreciated, thanks in advance!
try to used onchangesListener on realm. to sync your updated or new data to sql server..

Is there a way to set a scheduled task in an Android Application?

I'm working in an application that shows data from a MySQL database via PHP using WebServices with XAMPP and phpMyAdmin.
What I need to do now is a task or job that every 15 minutes or 'n' time set by the user configuration, makes a select to the database through a web service and return the data to show it as a notification in the phone.
Does Android have a way to do this kind of tasks or jobs?
I don't want to use Threads, because as far as I know, they tend to make the app slow with time and it's also a bad practice.

Want to know the best option to use for a database of this sort of android application

This is for an app for a clinic giving course in stop smoking and nicotine addiction,
We want to make sample app for android, where we scheduled the treatments course time plan, that’ remind the patients when to take the medicine.
So when the patient start their treatment, they just register at the day of starting the treatment, then automatically the app will scheduled all the following treatment, and send a reminder when next treatment arrive.
I know this topic is broad to answer but I just want to know how to make the database (schedules of the prescription) set for the timer to activate?? Can I use an external database that is network based..If so how to do that???Is sqlite a better option??? And how to automatically get those data and schedule it in the android app??
Since the data will be shown to all users like the time table so they can reserve if there is an empty date, then you require to have a cloud database and i suggest you to start learning how to connect android with MySQL here is a great video tutorial of how to connect android with MySQL by the help of PHP and JSON.
Now if you want this application to work offline, you require to have a SQLite database that when you connect to the internet once, you save these data in it. and whenever the device connects to the internet it updates the database entries.
for alerting the paitents you will have a service that runs to check the data , then start a notification. It may be possible to have an event listener for some given event that would start your service that would then check the date, and if so post your notification!
here is a good tutoial about it.
I hope I have covered most of your questions. There may be a better solutions around so dig more. Good luck with your app.

Android: Best way of handling continuous pulling from server?

To start of, i should mention that i'm a newbie in Android (Not that much experience in Java at all tbh), so be easy on me.
I am making an app that continuously pulls data from a server, and then returns data through a http post request. The question is, what is the best way to handle the actual pulling from the server? Should i be using AsyncTask or create another thread and let it run on that? Are there better methods for this purpose?
I will be pulling data every 5 minutes. (I am aware that this will drain the battery very fast, and i should definately be using Androids C2DM framework. But i have no experience in it before and i'm on a deadline, so this'll have to do until i have time to learn how to implement it.)
I'm grateful for any advice!
As an alternative to C2DM, you can do the persistent TCP connection between your device and the server. Then every 5 minutes your server can push a tickle to the device. Upon being tickled, the device can request the information via Http post.
Here is some sample code on how to do that.The connection stays open in a background thread even after the app has exited
Creating and Managing a persistent TCP socket: http://openmobster.googlecode.com/svn/trunk/cloud/android/connection/src/main/java/org/openmobster/core/mobileCloud/android/module/connection/NotificationListener.java
Full Disclosure: I am the Chief Engineer of OpenMobster and I wrote this code. Please feel free to use whatever you like or just get an idea if thats what you need
Thanks
Do you need to pull the data in background (even if your app is not "opened" and the android device is sleeping)? I suppose thats what you want because you mentioned C2DM. If so..the buzzwords are AlarmManager (with repeating time)/BroadCastReceiver and maybe NotificationManager to notify the user. With AlarmManager you schedule your events (every 5 minutes) and with BroadcastReceiver you receive those events and do what you want to do every 5 minutes :)

Categories

Resources