0

I had a problem about how to design updater for my program. Which the best order to update data from the server? (absolutely multithreading)

Thread {
   Timer {
      result = downloaddata(url)
   }
}

or

Timer {
   Thread {
     result = downloaddata(url)
   }
}
felangga
  • 553
  • 8
  • 22

2 Answers2

0

And who manges the timer in the second solution? Should it be again the main thread? I would suggest the first option.

In android the Thread will be implemented in a Service.

Also, think of whether you really need periodic updates. If the changes are relatively rare, then push notifications from the server might be better solution for your needs (they will greatly reduce the number of calls to the server, thus consumed resources).

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • I really dont understand about 'push notification', can you explain how it works ? or how to do it? – felangga Apr 13 '12 at 10:19
  • @FelixAnggaErlandhita I have linked in my answer to a thread in SO, that points you to `c2dms`. Basically how it works: when the server detects a change that should be propagated to the client he initiates a push that goes through the google cloud and finally is propagated to your application. The messages themselves are distributed the same way the gtalk messages and emails are distributed, thus reducing greatly the battery and network consumption. – Boris Strandjev Apr 13 '12 at 10:22
0

I suggest BroadCastereciever .. where it runs periodiacally and also in the Background.. you don't have create a Thrad there,.. and doing something in timer is not preferred because Timer is not that reliable.. IF your application goes to background it might get ended without any clue..

ngesh
  • 13,398
  • 4
  • 44
  • 60