Office Hours — Today, September 6

Tuesday, September 4

Sep 6
9:55 AM
Mark M.
has entered the room
Mark M.
turned on guest access
10:55 AM
tunneling
has entered the room
11:00 AM
tunneling
is it over?
Mark M.
it's about to be
do you have something really quick?
tunneling
I have a Service that runs a background thread. I'm binding to the service, and when I unbind the background thread is still running.
Mark M.
did you do anything to stop the thread?
tunneling
The onDestroy in the Service is getting called.
Mark M.
if you fork a thread, you have to clean it up
tunneling
clean it up in the onDestroy?
what if I need it to finish?
Mark M.
then you really should consider using startService(), instead of or in addition to bindService()
and you still need to make sure that the thread will eventually terminate
tunneling
ok, i was thinking that would be the case
Mark M.
if you bind, create a thread, and unbind without cleaning up the thread, it is no different than if you leak a thread from an activity that was destroyed by the BACK button
tunneling
ok, do you cover that in any of the books?
Mark M.
and the same risks exist (e.g., Android may terminate your process soonish, before your work is done)
tunneling
i think i've been pretty sloppy there
Mark M.
cover what?
tunneling
an elegant way to clean up based on the life cycles
Mark M.
not for your specific scenario, no
tunneling
ok. thanks Mark.
Mark M.
this is why I prefer the command pattern and IntentService
tunneling
oh
avoid the binding?
Mark M.
yes
tunneling
i've been binding so hard, it's hard for me to let go
Mark M.
IntentService will terminate the thread and shut itself down upon completion of the work
11:05 AM
tunneling
ok, i need to read up on that. that could be perfect for what im trying to do.
Mark M.
and that's fine, you are welcome to use binding, but it makes scenarios like yours a bit more complicated
and takes you outside the scope of what I try to cover
tunneling
im trying to download some remote data.. and just hook into the service to get progress updates
Mark M.
use LocalBroadcastManager
or regular broadcasts
or a Messenger
or something to deliver progress updates to your activity
tunneling
i've been trying to do it with the observer pattern
Mark M.
there is nothing in the observer pattern that requires binding
tunneling
yea.. i guess it's just they way im trying to implement.
Mark M.
LocalBroadcastManager, regular broadcasts, or a Messenger can implement an observer pattern
tunneling
i bind, then register the activity with the service as an observer, then notify the activity with "notifyObserver" methods
Mark M.
but that is just the observer
something is causing your service to "download some remote data"
whatever *that* is should use startService()
tunneling
i see... then the service can stopSelf() when the thread is done.
Mark M.
though personally I'd still use IntentService and the command pattern, but that's personal preference
correct
basically, the last thing the thread does is call stopSelf()
tunneling
got it
Mark M.
(I think there's a Wrath of Khan quote for this somewhere... :-)
tunneling
lol
Mark M.
anyway, I need to wrap up this chat
tunneling
thanks again for your insight. ttyl.
Mark M.
next one is Tuesday, 10am Eastern
11:10 AM
Mark M.
have a pleasant day!
tunneling
ok..ill try to be on time next time.
bye
tunneling
has left the room
Mark M.
turned off guest access

Tuesday, September 4

 

Office Hours

People in this transcript

  • Mark Murphy
  • tunneling