Mar 25 | 7:20 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Mar 25 | 7:25 PM |
EGHDK | has entered the room |
Mar 25 | 7:30 PM |
Mark M. |
hello, EGHDK!
|
Mark M. |
how can I help you today?
|
EGHDK |
View paste
|
Mark M. |
go right ahead!
|
EGHDK |
first, you mention closure a lot. Is that groovy specific for a block?
|
Mark M. |
well, the term "closure" isn't unique to Groovy
|
Mark M. |
and it's a trifle more involved than just a block
|
EGHDK |
Okay. Just making sure. I just wasn't sure reading the chapter and you were mentioning this closure or that closure, and wasn't 100% on what you meant.
|
Mark M. |
yeah, well, I probably should try to explain that term more in the book
|
Mark M. |
though it's really a Groovy syntax thing
|
EGHDK |
I guess in the context of that chapter, it was basically refering to it as a code block i guess right?
|
Mark M. |
more or less
|
Mark M. |
it's the stuff in { }
|
EGHDK |
Okay, I just wasn't sure, because i hadn't heard it before really. I have heard of clojure and wasn't sure if it was the same or something.
|
Mark M. |
I suspect that Clojure has closures, though I haven't really looked at it
|
Mar 25 | 7:35 PM |
EGHDK |
heh. Okay. cool. Next question.
|
EGHDK |
in the root build.gradle we include com.android.tools:gradle:verision#. Is that the gradle dependency number or the Android plugin for gradle version?
|
Mark M. |
the latter
|
Mark M. |
so most of my samples use 1.0.0
|
EGHDK |
Because gradle has a version number and so does the android plugin for gradle correct?
|
Mark M. |
correct
|
EGHDK |
Where is gradle version set?
|
Mark M. |
that depends
|
Mark M. |
for most projects, it is in gradle/wrapper/gradle-wrapper.properties, as part of the distributionUrl property
|
Raghu | has entered the room |
Mark M. |
hello, Raghu!
|
Raghu |
hi mark
|
Mark M. |
EGHDK: let me take a question from Raghu, and I will be back with you shortly
|
Mark M. |
Raghu: your turn! do you have a question?
|
Raghu |
yes
|
EGHDK |
No problem. I'll start typing my other questions out.
|
Mar 25 | 7:40 PM |
Raghu |
as i told u yesturday i am using a intent service to start download and upload in a queue
|
Raghu |
maximum limit for a queue is 6
|
Raghu |
i have maintin queue of size 6
|
Raghu |
upload is happening one by one
|
Raghu |
like in google playstore
|
Raghu |
when we download updates for apps in google play store
|
Raghu |
they happen in a queue
|
Raghu |
when we want cancel a particular item we can cancel the download
|
Raghu |
i wanted to implement similar to google play stores way of downloading updates
|
Raghu |
for each app
|
Mark M. |
OK... but what is your question?
|
Raghu |
wat is the best way to download in queue,maintain a queue and cancel or remove items from a queue
|
Mar 25 | 7:45 PM |
Mark M. |
I would start by not using IntentService
|
Raghu |
ok
|
Mark M. |
use a regular Service
|
Raghu |
ok
|
Mark M. |
use a LinkedBlockingQueue or PriorityQueue, depending on whether you need priority-based sorting or something
|
Raghu |
ok
|
Raghu |
its easy to remove items in linkedblocking queue,,?
|
Mark M. |
onStartCommand() would look at the Intent to see if it is a request to add a job or cancel an existing job
|
Raghu |
ohh ok
|
Mark M. |
LinkedBlockingQueue has a remove() method
|
Raghu |
linked blocking queue should be used in service
|
Mark M. |
for your purposes, yes
|
Mark M. |
onStartCommand() would then either put a job in the queue or find a job (based on some criteria, like an ID) and remove it
|
Raghu |
ok
|
Raghu |
how do i update the ui
|
Mark M. |
then, use an event bus (e.g., greenrobot's EventBus) to post events related to the jobs, such as completion status
|
Raghu |
ok
|
Raghu |
how do i send the request from the queue
|
Raghu |
iterate through the queue
|
Raghu |
service will start a thread..?
|
Mark M. |
you use the LinkedBlockingQueue with a ThreadPoolExecutor, with just one thread (since you want only one download at a time)
|
Raghu |
yes exactly
|
Mark M. |
if you need this queue to survive process termination, either use Square's Tape library or come up with your own persistence model
|
Raghu |
ok
|
Mark M. |
and with that, let me take a question from EGHDK, and I will be back with you in a bit
|
Mark M. |
EGHDK: your turn! do you have a question?
|
Raghu |
sure
|
EGHDK |
just to piggyback here. How do you know of all the best collections for a specific task. just reading docs? or any good collections book?, second what if you didn't want to use an event bus?
|
Mar 25 | 7:50 PM |
Mark M. |
I'm sure that I don't know all the best collections for a specific task
|
Mark M. |
I have my go-to set of collection classes, ones that usually fit the way my brain works from a design standpoint
|
Mark M. |
and you're either going to use an event bus or you are going to write your own event bus equivalent from scratch, which seems like a waste
|
EGHDK |
would that revolve around broadcastrecivers?
|
Mark M. |
there aren't great options for updating a UI from a service's thread without eventually getting to an event bus of some form
|
Mark M. |
LocalBroadcastManager is one event bus implementation
|
Mark M. |
it's not the one I'd recommend in general, but it does qualify
|
EGHDK |
Oh. LocalBroadcastManager IS an event bus
|
Mark M. |
yes
|
EGHDK |
didn't know that.
|
Mark M. |
we used to use system broadcasts for that role, which was bad bad bad bad bad
|
Mark M. |
and so they gave us LocalBroadcastManager as a way to easily migrate from the bad stuff to a better approach
|
Mark M. |
but, as event bus implementations go, it's not the most feature-rich and it's a bit clunky to code to
|
EGHDK |
Interesting that when they built the platform that they didn't build LocalBroadcastManager then... huh?
|
EGHDK |
or that they didn't put in a good way to update UI
|
Mark M. |
there are lots of things that weren't in Android 1.0
|
EGHDK |
True. That's why its a 1.0
|
Mark M. |
oh, there are lower-level things, like the Handler/Messenger combination
|
EGHDK |
Anyway. Good question Raghu
|
EGHDK |
Okay back to my actual questions. Will I ever have to update the gradle version...? or only if the android gradle plugin updates and uses something that my current gradle version doesn't know about.
|
Mark M. |
"Will I ever have to update the gradle version...?" -- someday, probably
|
Mark M. |
"or only if the android gradle plugin updates and uses something that my current gradle version doesn't know about" -- yes
|
Mark M. | |
EGHDK |
You can go back to raghu after that. I know I kinda took up my time with event bus/collections. I'll keep jotting down my other questions.
|
Mark M. |
that has Google's plans for the relationship between Gradle version and Gradle for Android version
|
Mark M. |
OK
|
Mark M. |
Raghu: back to you -- do you have another question?
|
Raghu |
sorry i have more queries on first question
|
Mar 25 | 7:55 PM |
Raghu |
is that fine mark
|
Mark M. |
sure
|
Raghu |
service processes one request(using a thread pool) and we use event bus
|
Raghu |
to update
|
Raghu |
how will i start the next download
|
Raghu |
1,2,3,4,5
|
Raghu |
1 completes
|
Raghu |
how do i move to 2
|
Raghu |
2 to 3
|
Mark M. |
you put 1, 2, 3, 4, 5 in the queue
|
Raghu |
ok
|
Mark M. |
everything else, we already discussed
|
Mark M. |
you use ScheduledExecutorService with the LinkedBlockingQueue
|
Mark M. |
it gives you control to do the download
|
Raghu |
ok
|
Mark M. |
if you put 1, 2, 3, 4, 5 in the queue, it will give you control five times, to do 1, 2, 3, 4, 5
|
Raghu |
yes
|
Raghu |
activity will have to trigger in the response or scheduleexecutor knows that there are more items
|
Raghu |
to process
|
Raghu |
right
|
Mark M. |
activity will call startService() to send the request to do the download to the service
|
Mark M. |
service gets control in onStartCommand(), puts an item in the work queue
|
Mark M. |
when the item comes to the head of the queue, the thread from the ScheduledExecutorService gives you control to do the work
|
Mark M. |
and part of that work can be posting events on an event bus, or using a Messenger, or whatever, to update your UI
|
Mar 25 | 8:00 PM |
Raghu |
this thread is responsible for sending and handling the response
|
Raghu |
and also updating the Ui
|
Mark M. |
this thread is responsible for doing whatever background work you wanted to have done via the queue
|
Raghu |
yes
|
Mark M. |
and this thread is responsible for notifying the UI (event bus, Messenger, whatever) about anything the UI needs to know about
|
Raghu |
thanks mark
|
Raghu |
i am clear
|
Mark M. |
let me take a question from EGHDK, and I will be back with you in a bit
|
Mark M. |
EGHDK: back to you!
|
EGHDK |
Okay, tell me if I'm correct. Maven Central is a remote repository that holds thousands of artifacts. Apache Maven is a build system. Maven is a type of way to set up an artifact.
|
Mark M. |
the first two are solid
|
Mark M. |
I'd phrase the third one more as "Maven Central implements what is known as a Maven-style repository for artifacts, with specific rules for metadata formats, directory structures, etc."
|
Mark M. |
there are other artifact repository formats in the Java world (e.g., Apache Ivy), though Maven has largely won out in the marketplace of implementations
|
EGHDK |
So can I replace "Maven Central" in that statement with JCenter?
|
Mark M. |
yes
|
EGHDK |
I'm just confused as of why https://bintray.com/bintray/jcenter shows a "Maven" logo right on that page.
|
Mar 25 | 8:05 PM |
Mark M. |
JCenter is a Maven-style repository
|
Mark M. |
"JCenter is the place to find and share popular Apache Maven packages for use by Maven, Gradle, Ivy, SBT, etc. "
|
Mark M. |
an "Apache Maven package" is the combination of the actual artifact itself (e.g., a JAR) and some metadata about the artifact (a POM file)
|
EGHDK |
okay, so maven central is crystal clear now... but is apache maven a build system that can build apache maven packages?
|
Mark M. |
Apache Maven is a build system in general
|
Mark M. |
same basic roles as Apache Ant, Gradle, etc.
|
Mark M. |
they popularized the notion of artifacts and repositories in the Java space
|
Mark M. |
their build system itself is reasonably popular, and much more powerful than is Ant
|
EGHDK |
gotcha. okay. that helps a bit. thanks
|
Mark M. |
that's one of the problems in this area: "Maven" is used for lots of related but disparate things
|
Mark M. |
let me take another question from Raghu, and I will be back with you shortly
|
Mark M. |
Raghu: your turn! do you have another question?
|
Raghu |
no mark thanks for the solution
|
Mark M. |
OK
|
Mark M. |
if you come up with another question before the chat ends, let me know
|
Raghu |
sure
|
Mark M. |
EGHDK: over to you!
|
EGHDK |
In your book you mention if you want to use maven central you can add it in your app module build.gradle. And your example does just that. But why wouldn't you just put that right under where jcenter() is entered by default?
|
Mark M. |
you usually don't need both jcenter() and mavenCentral()
|
Mar 25 | 8:10 PM |
Mark M. |
you need one or the other
|
Mark M. |
in a new Android Studio project, it will be set up with jcenter()
|
Mark M. |
that didn't use to be the case
|
Mark M. |
earlier versions of Android Studio used mavenCentral()
|
Mark M. |
they switched around the time of Android Studio 1.0, or perhaps a bit before
|
Mark M. |
so you'll see bits of both in the book samples, but more mavenCentral(), due to the timing of when I did the work
|
EGHDK |
gotcha. But lets say one dependency I want is on maven central and not bin tray... I could put that in just the root build.gradle then right?
|
Mark M. |
yes
|
Mark M. |
you might consider replacing jcenter() with mavenCentral(), though, rather than having both
|
EGHDK |
cool.
|
EGHDK |
Back to something that got my interest. event bus.
|
EGHDK |
So I don't use services that often, so I don't know how I would communicate back to an activity, thats why Im asking these next questions.
|
EGHDK |
I thought services could be bound or unbound (i was taking that as "bound" or "unbound" to the gui)
|
EGHDK |
So you said localBroadCast is something you can use right now without any adiitional library right?
|
Mar 25 | 8:15 PM |
Mark M. |
well, LocalBroadcastManager is in the Android Support package
|
Mark M. |
so you would need support-v4 or support-v13
|
EGHDK |
You said that before that you would use System Broadcasts?
|
Mark M. |
back in the 2009-2011 timeframe, that was one approach we erroneously used
|
EGHDK |
How did you use a system broadcast? aren't system broadcasts for like low battery and phone call and boot complete and stuff?
|
EGHDK |
Why erroneous?
|
Mark M. |
when I use the term "system broadcast", I mean methods like sendBroadcast() on Context, where the receivers could be in any app
|
Mark M. |
in contrast to a "local broadcast", where sendBroadcast() is on LocalBroadcastManager
|
Mark M. |
system broadcasts are a cross-process event bus
|
Mark M. |
however, from a security and performance standpoint, never use a cross-process X when you can achieve the same results using an in-process X
|
Mark M. |
and I, and others, weren't thinking of those implications back then
|
Mark M. |
we were young(er) and stupid
|
EGHDK |
Gotcha. So security and performance.
|
Mark M. |
right
|
EGHDK |
Makes sense. Don't want any app to catch your broadcast and do something you didn't want.
|
Mark M. |
now, if you need a cross-process event bus, system broadcasts are your best option, but you just have to work out the security ramifications
|
Mark M. |
right
|
Raghu |
can i ask a last question on linkedblockqueue mapping with the Ui(activity).
|
Mark M. |
Raghu: go ahead
|
Mar 25 | 8:20 PM |
Raghu |
I wanted to cancel nth item(click on cancel from UI),how do I map the clicked item with the linked blocking queue in service so that i can remove it from the queue
|
Mark M. |
you need some sort of identifier
|
Raghu |
yes
|
Mark M. |
could be the URL of the download
|
Mark M. |
or a generated ID
|
Raghu |
this url is part of queue
|
Raghu |
in a obect
|
Raghu |
object
|
Mark M. |
I was under the impression that your background work involved downloads, which is why I suggested a URL
|
Mark M. |
but it would be part of the work that you would put into the queue
|
Raghu |
yes exactly
|
Mark M. |
ideally, those objects would be from some class (call it Job for now) that has a proper implementation of equals()
|
Raghu |
we can find the item from the identifier
|
Mark M. |
that does the comparison by ID
|
Mark M. |
then, you can use remove() on a Job with the same ID to remove the real actual Job instance that is in the queue
|
Raghu |
ok
|
Raghu |
job has equals and hashcode
|
Mark M. |
remove(), contains(), etc. on LinkedBlockingQueue use equals(), rather than object identity, for finding things in the queue
|
Mark M. |
right
|
Mark M. |
so, the activity calls startService() with a "cancel this job" Intent, with the identifier
|
Mark M. |
onStartCommand() creates the Job with that identifier and calls remove() on the LinkedBlockingQueue, thereby removing any Job for that identifier
|
Mar 25 | 8:25 PM |
Mark M. |
EGHDK: if you have other questions, go ahead, as we are running out of chat time
|
EGHDK |
You mentioned a lower level event bus? Message and Handler?
|
Mark M. |
it's not really an "event bus", per se, but it is a way for disparate components to communicate
|
Mark M. |
I know you have worked with Handler, based on prior chats
|
Mark M. |
you can create a Messenger tied to a Handler
|
Mark M. |
Messenger can go in things like an Intent extra
|
Mark M. |
so a service's client (e.g., activity) can have a Handler, create a Messenger, and pass that Messenger to the service
|
Mark M. |
the service can hold onto that Messenger and send messages back to the client
|
Mark M. |
doing this right requires some amount of garbage-collection bookkeeping
|
EGHDK |
Okay, so it is pretty low level. Is that what these other "good" event buses are built off of?
|
Mark M. |
in the end, they all wind up using a Handler
|
EGHDK |
Even localBroadcastReceiver?
|
Mark M. |
actually, that one might not
|
Mark M. |
no, no, it should
|
Mark M. |
Otto is the one that might not
|
Mark M. |
it comes back to threading
|
Mark M. |
LocalBroadcastManager always delivers events on the main application thread, and so it is using a Handler tied to that thread
|
EGHDK |
Okay, I'll look into it. I have to start using services. At this point, I don't see where I would use an event bus except service to activity interaction
|
Mark M. |
BroadcastReceiver to UI interaction too
|
Mark M. |
particularly for OS-initiated broadcasts that you have a manifest-registered receiver for
|
Mar 25 | 8:30 PM |
EGHDK |
Main application thread has a single handler... or is it an Activity that has its own handler?
|
Mark M. |
Handlers are tied to threads
|
EGHDK |
or both... I guess?
|
Mark M. |
there can be N Handler instances associated with the main application thread
|
Mark M. |
an activity usually does not need more than one
|
Mark M. |
and that's a wrap for today's chat
|
EGHDK |
Gotcha. Okay, cool I will read about them. Super interesting stuff here.
|
Mark M. |
the transcript will go up on http://commonsware.com/office-hours/ shortly
|
EGHDK |
appreciate it mark. Till next chat! Thanks
|
Mark M. |
the next chat is Tuesday, also at 7:30pm
|
Mark M. |
have a pleasant day!
|
EGHDK | has left the room |
Raghu | has left the room |
Mark M. | turned off guest access |