Reputation: 99
I am creating an android app where one user will be able to see another user on the map.
Here's the thing
There's a web service, the client request all the logged-in users from the web service then renders the users as a marker on the map. After every few seconds it makes a request again.
I've got the client side figured out and working.
The problem lies on the server side. The way i am doing it is:
It works. But I know it's not the way to do it.
Here's the problem areas:
How can I improve the server logic?
Upvotes: 1
Views: 552
Reputation: 5542
You will have to create a new Thread so that the call don't lock up the device if the call takes longer than expected. The AsyncTask is an easy way to use multithreading, but it lacks the functionality of repeating tasks. I would say that you are best of either using a Timer
or the newer ScheduledExecutorService
. If you chose to use the Timer you create a TimerTask that you can hand it. The ScheduledExecutorService takes a Runnable instead.
You might want to wrap the thread in a Service (The Service does not provide a new Thread), but this is not always necessary depending on your needs.
As suggested , you can also use the Handler.postDelayed(). Although you still need to create a new thread and then call Looper.prepare() on it:
class LooperThread extends Thread { public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
} (Code from Looper docs)
Also; calls to a webservice every 5 second seems way too frequent, especially if the user is on a slow connection or there are data that needs to be transferred, try to reduce the calls as much as possible.
Upvotes: 2
Reputation: 1923
use Google Cloud Messaging service. Requesting every 5 second is a bad idea, and will drain battery too.. Only changes needs to be updated from server to client. So Push notification is the right way.
In your example - Not all the 100 users location will be changing every 5 seconds. Also normalize the change -(say few change in few meters can be ignored - no need to notify client)
In case you are going by polling mechanism, server can sent all the location update with one rest API.
Upvotes: 0