Mark M. | has entered the room |
Mark M. | turned on guest access |
Jul 7 | 4:00 PM |
RobertoAllende | has entered the room |
Mark M. |
hello, Roberto!
|
Mark M. |
how can I help you today?
|
RobertoAllende |
Mark, how are you doing ?
|
RobertoAllende |
I am reading your book...
|
RobertoAllende |
great stuff (btw)
|
Mark M. |
thanks!
|
RobertoAllende |
and i am working on an app similar to Expedia (booking tickets and stuff like that)
|
RobertoAllende |
which basically consists on querying and sending data to APIs in a sequential steps...
|
RobertoAllende |
so, the thing is I am thinking on the threading part
|
RobertoAllende |
I think i could use AsynTasks, i am aware of the issues and also i think i can manage them
|
Jul 7 | 4:05 PM |
RobertoAllende |
but i don't think they're the best way... i was also considering IntentServices
|
RobertoAllende |
but i am curious because there are not much in your book about them
|
Mark M. |
IMHO, there is a fair bit in the book on IntentService
|
RobertoAllende |
so i just wanted to ask you about them...
|
Mark M. |
at least as much as there is on AsyncTask
|
Mark M. |
that being said, I am happy to try to answer any questions you have about IntentService
|
RobertoAllende |
This stage is more from architecture point of view rather than specific issues
|
RobertoAllende |
So, I am considering to use something like retrofit, picasso...
|
RobertoAllende |
I just need to close the threading part...
|
RobertoAllende |
it's quite open...
|
RobertoAllende |
i don't know if it's ok to ask so wide questions on this chat sessions...
|
Mark M. |
well, Retrofit and Picasso are designed to handle threading for you
|
Mark M. |
well, you have not really asked a question yet :-)
|
RobertoAllende |
Ok, let me put in a question way then :)
|
Jul 7 | 4:10 PM |
RobertoAllende |
i've to do an app to consume APIs, i plan to use picasso and retrofit. The API calls are sequentials, ie until i don't finish a step i won't do another API call. How would you recommend to manage non-ui part, would you leave it to the libraries (using their callbacks), would you use them in synchronous-mode and use something like asynctasks, intenteservices ?
|
Mark M. |
in the case of Retrofit, you certainly have the synchronous option (I don't recall seeing one in Picasso...)
|
Mark M. |
so, from a transaction sort of mindset, I would go the synchronous route
|
Mark M. |
as that, IMHO, will give you easier code to read than some chain of listeners, with one listener invoking the next step in the transaction
|
Mark M. |
with respect to AsyncTask versus IntentService, here is my general rule of thumb:
|
Mark M. |
1. if the work will take less than a second, and you do not mind if the work is discarded should the activity be destroyed, feel free to have an activity (or one of its fragments) use an AsyncTask
|
Mark M. |
2. if the work will take 1+ seconds, or you want to ensure that it runs to completion, use an IntentService
|
Jul 7 | 4:15 PM |
Mark M. |
3. if the work will take 15+ seconds, now you have to start thinking about my WakefulIntentService, so the device stays awake while all of this is going on
|
Mark M. |
the 15 second value comes from the minimum automatic screen timeout value in Settings
|
Mark M. |
as a result, given this set of rules, I usually reserve AsyncTask for light stuff needed by the UI layer
|
Mark M. |
but "serious" work, such as updating a server, probably should use a service of some form
|
Mark M. |
now, IntentService has its own limitations (e.g., only one thread per service), so you may need to replace it with your own service that calls stopSelf() when appropriate
|
RobertoAllende |
Ok...
|
Jul 7 | 4:20 PM |
RobertoAllende |
I am thinking on the 'only one thread per service' restriction... do have an example where that might be a problem ? at the moment i cannot figure out where that might be an issue
|
Mark M. |
suppose your transaction consists of 5 Web service API calls
|
Mark M. |
call these A, B, C, D, and E
|
Mark M. |
the rule is that you have to do A first (e.g., create an invoice), and you have to do E last (e.g., mark the invoice as ready for processing)
|
Mark M. |
B, C, and D are calls that have to happen in between A and E (e.g., add line items to the invoice)
|
Mark M. |
but B, C, and D do not depend upon one another, and so could be performed in parallel, if your Web service supports it
|
Mark M. |
with an IntentService, and only one thread, you have two key limitations:
|
Mark M. |
1. you cannot readily do B, C, and D in parallel, so you would wind up serializing A through E, which would take more wall-clock time
|
Mark M. |
2. you cannot do another independent transaction while processing an existing one; it will queue up instead
|
Mark M. |
now, if neither of those are a particular problem for you, IntentService is easy to use
|
Mark M. |
but, if either of those are a problem, you will need a more sophisticated threading model within your own subclass of Service instead of IntentService
|
Jul 7 | 4:25 PM |
RobertoAllende |
I see. Even if I do an IntentService per API call, A, B, C, D and E. Would I have the same restriction ? which I guess it would be the same queue for all the intent services (if that's correct?)
|
Mark M. |
each IntentService instance has its own thread, but services are natural singletons
|
Mark M. |
so, if you have IntentServiceA and IntentServiceB and so on, they will run in parallel
|
Mark M. |
however, in my scenario, you do need to do some serialization (e.g., B/C/D have to be after A, E has to be after B/C/D)
|
Mark M. |
that could be by one service starting another one, but no matter how you approach it, complex business rules result in some amount of code complexity
|
RobertoAllende |
"complex business rules result in some amount of code complexity" that's a very good one.
|
Jul 7 | 4:30 PM |
RobertoAllende |
Ok... Fortunately I've some time to decide, so i'll keep researching a bit about it, doing some experiments and perhaps I come back with this topic in the near future.
|
RobertoAllende |
Thank you very much for your service... i am very surprised about what you're doing, i consider your business awesome. Keep doing it!
|
Mark M. |
I am happy to be useful
|
RobertoAllende |
Bye, bye, have a good time.
|
Mark M. |
have a pleasant day!
|
RobertoAllende |
you too
|
Jul 7 | 4:50 PM |
RobertoAllende | has left the room |
Jul 7 | 5:00 PM |
Mark M. | turned off guest access |