1

I have an android project that uses parse for its backend. To cut the number of api calls, I had been pinning "not so important" data to the local data store and syncing it once in a day using AlarmManager and BroadcastReceiver.

Now I want to sync the data often, every 2/3 minutes only when the app is running. How do I go about it? Do I use the same AlarmManager and BroadCastReceiver(changing the frequency and cancelling alarm on exit) to implement this? Or is there a more efficient way?

P.S : Parse.com users, what are the best ways to reduce the api call count in such a scenario? Are there any elegant ways?

I have a Posts table, which has texts posted by users and a votes table. Whenever a user creates a post I create a Posts object and save it to parse. And when another user uses the app, he gets to vote. The vote count for every Post object is saved in a column in the Posts table as and when the voting is done. But the user specific choices are saved locally. This is to reduce the number of API calls, I accumulate the user choices over time and sync it periodically.

55597
  • 2,033
  • 1
  • 21
  • 40
  • One possible way for API level 21+ is to use the new `JobScheduler` https://developer.android.com/reference/android/app/job/JobScheduler.html. It really depends what kind of app it is, respectively how long and often an average user will kepp the app in foreground – momo Jun 11 '15 at 16:28
  • How can I start a service everytime my app is closed? – 55597 Jun 12 '15 at 15:44

2 Answers2

0

The simpelest way would probably Alarm Reciver filter "ACTION_TIME_TICK" How ever I'm not sure about the performence (it would tick even if the app hasn't start). Anyway I think you could work with treads, make a service which would run a Handler every 2-3 minutes, and start it when you application is up, and ofcourse terminate it when you application is being closed.

You might also look HERE.

Community
  • 1
  • 1
Remy
  • 1,053
  • 1
  • 19
  • 25
0

I use Parse.com in some applications, I think the best way it depends on your application. For example, you can sync every "Start" in your application and also have a kind of AlarManager to be fired each hour,for example.

I don't know about your application, but I used to synchronize the data in each change on the server. So, once I have a change on my server, I send a notification and update my data on mobile, but it depends on how your application needs to sync that, if it will be a sync from mobile to server or server to mobile. I would like to hear from you more specific about your application and we can try a good approach for you.

Franzé Jr.
  • 1,194
  • 13
  • 20
  • @55597 The problem with this cache you are doing, it's because another user cannot have the the last update, did you think about that? If it's not a problem for you, your solutions looks good. But I think using this model is better always sync and make Parse calls to all users be notified. What do you think ? – Franzé Jr. Jun 11 '15 at 17:08
  • The voteCount gets updated synchronously, thats is the only data shown to all users. The userspecific choices are used to change the button states in the same user's app. Therefore it is not important to keep it synchronous. I back it up only to know the state when the app cache is voluntarily cleared by the user. In this case, i will need the user's votes that he already voted to reflect it on his app. – 55597 Jun 11 '15 at 17:14
  • @55597 I got it. So, it sounds like you are using a good approach, because you are keeping local the information and periodically you send all of this, decreasing the number of APIs calls. In my application, to reduce the number of API Calls, I have a local database too and when the user makes a change, I make the Parse.com calls, only if the user makes changes. Going back to your approach I think it's very simple and seems good. – Franzé Jr. Jun 11 '15 at 17:20