简体   繁体   中英

Syncronizing Android client with remote SQL-server database, using web service

I implemented an Android application that requires a set of data, taken by a SQL Server database. The application obtains the data calling a WS. After a first call to WS when the application start the first time, I need to maintain the data updated, according to the modify that may happens server-side (SQL server database). For obtaining this result I perform, with a with a predefined frequency, a WS call, for knowing if data on database are changed. If new data are available, other web service is called for obtaining them.

This solution works fine for my ( I don't require real-time update). But, I think that this solution is too expensive in term of energy consumption, cpu consumption and network traffic. Since, I immagine this is a very common problem I would know if exists a generic way to deal with it.

What you do is ok for most cases. You can take advantage of Google Cloud Messaging , but it needs time and effort to get implemented. I would stay with your solution.

I suggest you to use extra fields. Add four colums to your local tables in Android :

  • TRANSACTING_FLAG : Set it to true when you are posting or updating this resource on the server
  • REQUEST_STATE : Set this flag to POSTING / UPDATING / DELETING etc.
  • RESULT_CODE : Set this field to the last result code received by the server for this particular resource.
  • TIMESTAMP : Period after wich data has to be updated

Workflow is simple : When you retrieve data for your server just check if the last updated timestamp of your resource is superior to the cache timestamp you have defined before. If the timestamp is superior perform a request to update data. The transacting boolean let you know that a particular resource is actually synchronizing with the server. The result code lets you know if the request has failed or not and enventually retry it when the network is available. Doing this way you will maintain the persitence between your local and remote database because at any moment you can check the "synchronized state" of any local resource thanks to extra fields seen before.

I made a library to handle this. Take a look to RESTDroid . Even if the cache functionnality is not handles, you will be able to simply add it.

You could look into Query Notifications, using something like SqlDependency - http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency.aspx . You can use this to track whether the results of a query change and to inform an application when this happens.

There are restrictions on the query you can use, and cost on the server is similar to an indexed view. You need .NET for this, by the way. If implemented in your Web Service, you would have to implement some kind of subscribe feature for your android, so that notifications could be pushed to it.

Another option to reduce the cost of checking for changes could be SQL Server Change Tracking - http://msdn.microsoft.com/en-us/library/bb933875.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM