Hades
Hades

Reputation: 3936

how to handle updates from the server - sync

I have a JSON response,

Basically a list of categories

[{event_cat_id: "1", event_cat_name: "Sport" 
}, { event_cat_id: "25", event_cat_name: "Cinema" }]

using the ID, I query the server for a list of events.

All the events sent back are stored in a local DB.

My problem is that the list of categories can change, I want to know what is the best of way of checking for updates on the server.

I could send a request every time the view is clicked? Would that affect the user experience?

Building on android

Upvotes: 0

Views: 317

Answers (3)

pawelzieba
pawelzieba

Reputation: 16082

On server side keep timestamp of last update for each record and property if record is available
ex:

id: 25, name: Cinema, available: 1, timestamp: 1342777317
  1. Get server current time
  2. Get data from server modified since last synchronization time
  3. Parse and store data in local database
  4. Write server time from 1. point as last synchronization time

It's easy to query data on server modified since a timestamp. At first synchronization last synchronization time is simply 0.
You can't delete records on server side, you can set them as unavailable: available: 0

Upvotes: 1

Anand
Anand

Reputation: 1325

Whatever I understand from that, I would like to suggest you to use C2DM Push notification whenever any updates or category available on server database. it will push message to device and device code will try to pull the data from server url.

by this way every time user device can not ping to server and it will reduce the load from server as well as save battery(drain battery) of device.

Upvotes: 0

Fernando Gallego
Fernando Gallego

Reputation: 4116

You can use the AlarmManager to set inexact repeating events that fire your server requests, that way your info is updated frequently but it saves battery. You should trigger the server request and response in a background thread, using a service or a intentservice. You can notify your activities using broadcasts or a ContentObserver for your database as soon as something changes.

Here is a good example http://www.dotkam.com/2011/01/10/android-prefer-alarms-and-intent-receivers-to-services/

Upvotes: 0

Related Questions