Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syncing with server

I've been developing for Android for some time now and currently working with syncing with my server.
Quick introduction: I've read about Sync Adapter and do know its role but I just want to know if it's my best option.

In my app user can click on a button to notify something is important to him. On click I'd like to update the server to keep the data, as well as notify his relatives. My problem comes as follows: The user can click that said button several times switching it on and off so in that case I don't want to open communication with the server each time the user clicks a button, it might not even change state from its previous state.

So my question is: Is sync adapter the right thing for me or should I build my own class to talk to the server that will check that, as well as receive urgent\non-urgent like Volley's. If someone could clarify the exact work of Sync Adapter I'd really appreciate it (Does it do what I stated?)

If something isn't clear i'd be happy to clarify. Thanks in advance.

like image 640
user2558461 Avatar asked Mar 04 '26 08:03

user2558461


1 Answers

Android sync framework should be pretty good for your use. If you mark a sync as needed, the system will not launch it right now, but delay it for a small time. This way, if the user switch back the button, you'll be able to notice that no value has changed. Sidenote: if you use a ContentProvider, it notifies the SyncAdapter automagically when data changes. You can also trigger a sync manually if needed.

What the sync framework do is just calling a method (onPerformSync). You're free to implement it as you want. You can skip a sync if you think the last one was too close, or find if data has really changed, or it's back to original value (you can for example keep the "last synced" value in your database, then you can compare it to the current value in your onPerformSync method. If it's the same, no change).

Bonus: you'll save battery power, as the sync framework run multiple syncs in sequence as much as possible, your users will be able to manage their accounts in a centralized place (Android settings), disable sync system-wide (think about Sony's stamina-mode, setting every SyncAdapter in pause while active).

like image 195
Marc Plano-Lesay Avatar answered Mar 05 '26 21:03

Marc Plano-Lesay