简体   繁体   中英

Best way to access database from android

I am working on a Android app and I have a dilemma. I have a list of Objects. I have to update each of these objects with a database. I have 2 methods:

Method 1: I can loop through the Objects. For each object I can connect to the server, update it, and then move on to the next Object, and so forth.

Method 2: I can store the Objects in a list, send the whole list to the server, update it on the server side, then return a list of updated objects.

My questions are:

  1. Which method is faster?
  2. Which method is easier on the phone's battery?

By the way, Method 1 is easier for me to code :). Thank you.

Method 1: Will take a little longer and utilize more bandwidth and processing power
Reasons:

  • Each network call will have its own overhead of socket creation and closing.
  • Each network call may have to wait until socket is opened and handshake properly done.
  • Each "update" may have its own overhead in terms of message that you pass. For example, if you pass XML data to server, enclosing tags etc may need to be repeated for every object.
  • Serialization of each object (in XML, native or otherwise) will tend to have additional overheads each time

Method 2: Will generally take less time and tend to utilize less bandwidth and processing power
Reasons: See above :)

I don't think that there's going to be much of a difference (depends on the size) but method 2 will be more efficient, since you have less overhead of starting the process, negotiation, et cetera. I would go with method 2 always.

But performance-wise, I don't think it will really matter a lot.

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