Dec 13 | 3:55 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Steve S. | has entered the room |
Dec 13 | 4:00 PM |
Steve S. |
Hi, Mark!
|
Mark M. |
hello, Steve!
|
Mark M. |
how can I help you today?
|
Steve S. |
Let me paste in a question:
|
Steve S. |
I recently saw a crash due to a TimeoutException in a call to a finalize() method made by the OS. Based on Stackoverflow, this seems to be due to a bug in the Android garbage collection code that can't be avoided, though it can be made less frequent. Do you know anything about this bug beyond what's on Stackoverflow? Would this be a reason to use 5.0 instead of 4.3/4.4 (we provide the devices our customers use)?
|
Mark M. |
um, I can't really comment on that in the abstract
|
Mark M. |
having never seen that exception personally
|
Mark M. |
do you have a stack trace?
|
Steve S. |
ok. so this isn't a problem you were aware of?
|
Steve S. |
one moment
|
Mark M. |
I believe that this is the first that I've heard of it
|
Steve S. |
View paste
(7 more lines)
|
Mark M. |
are you sure that you are closing all of your BluetoothSockets yourself?
|
Mark M. |
before they go out of scope and become eligible for GC?
|
Mark M. |
beyond that, this feels like a bug in BluetoothSocket, more so than the GC engine
|
Steve S. |
No, that's not something I do since the Bluetooth code is supposed to be running all the time. But maybe it's something I should be doing.
|
Mark M. |
some BluetoothSocket is going out of scope; otherwise, nothing would be calling finalize() on it
|
Steve S. |
Here's a link to the Stackoverflow post:
|
Steve S. | |
Steve S. |
I had thought what I saw was the same bug, but maybe not
|
Dec 13 | 4:05 PM |
Mark M. |
it's certainly possible that there's a problem with the GC stuff
|
Mark M. |
however, GC is used in 100% of Android apps
|
Mark M. |
Bluetooth is used in <1% of Android apps
|
Mark M. |
that leads me to blame Bluetooth over GC
|
Steve S. |
ok. you suggested i might need to close the socket myself when BluetoothSocket goes out of scope. If I implement
|
Steve S. |
Bluetooth using a service, could I accomplish that by closing the socket in onDestroy()?
|
Mark M. |
sure
|
Mark M. |
either onDestroy() will be called, your process will be terminated, or you had an unhandled exception
|
Mark M. |
IIRC, your app is on a device that is constantly on power -- correct?
|
Steve S. |
yes
|
Mark M. |
then the scenario in the "Now consider the following scenario:" section of the top-rated answer shouldn't be one you encounter
|
Mark M. |
as the device should not be going to sleep
|
Dec 13 | 4:10 PM |
Steve S. |
right. this is something i saw when i was doing something else, so it's not representative of the intended use case
|
Steve S. |
ok. so it sounds like a strategy for avoiding the problem i saw could be to close the BluetoothSocket in onDestroy() in case that gets called, but to always keep the device on in case it's the problem described on Stackoverflow
|
Mark M. |
well, the decision of whether or not to keep the device powered on is probably going to be made based on something other than an occasional GC-triggered TimeoutException :-)
|
Mark M. |
basically, I can think of two reasons why that TimeoutException occurs:
|
Mark M. |
1. GC itself blocks, which is the "device falling asleep" scenario outlined in that answer
|
Mark M. |
2. BluetoothSocket's finalize() gets stuck
|
Mark M. |
my hope is that if you close the BluetoothSocket, the odds of finalize() getting stuck decrease
|
Mark M. |
but, that's just an educated guess
|
Dec 13 | 4:15 PM |
Steve S. |
ok. we have independent reasons for keeping the device on all the time. so maybe between that and closing the socket in onDestroy we'll be ok.
|
Steve S. |
i have a second question on a different topic. i'll paste it in:
|
Steve S. |
I'm working on an activity with a large number of views. For performance reasons, I'd like to avoid repeating calls to findViewById() for the same views, but having a member field for each view gets messy due to the large number of views. Do you have any general suggestions about how to approach this? Would the Android data binding framework be something to look at?
|
Mark M. |
um, well, you could use the data binding framework if you wanted
|
Mark M. |
in the end, what it will do (among other things) is code-generate a Java class with fields that will hold all of your widgets that have distinct android:id values
|
Mark M. |
and fill those fields in when you inflate the layout using the associated Binding class
|
Mark M. |
Buttter Knife would be another option: https://github.com/JakeWharton/butterknife
|
Mark M. |
leastways, I think it has an option to have your annotated fields be on a separate POJO from the activity
|
Dec 13 | 4:20 PM |
Mark M. |
usually, my reaction to this is: are you really sure that having all of those widgets in a single unit of UI (activity, fragment) is the right answer?
|
Mike F. | has entered the room |
Mark M. |
if it's tedious for you to have lots of fields, it's probably tedious for the user to wade through all of the widgets
|
Mark M. |
let me take a question from Mike, and I'll return to you in a bit
|
Steve S. |
sure
|
Mark M. |
Mike: hi! welcome to the chat! do you have a question?
|
Mike F. |
hi! Yes, something I was wondering about Mark. Look at the section of the book where you discuss Retroft, i.e. p 730 or so...
|
Mark M. |
OK
|
Mike F. |
I was wondering where one would look up more methods that an object has, such as the so rest adapter.
|
Mike F. |
would you find that on the Retrofit site?
|
Mark M. |
yes, in one form or another
|
Mark M. |
for example, here are the JavaDocs for Retrofit 2.x: http://square.github.io/retrofit/2.x/retrofit/
|
Mark M. |
I got those by visiting the GitHub repo (https://github.com/square/retrofit), not seeing them there, then clicking on the "the website" link (leading to http://square.github.io/retrofit/)
|
Dec 13 | 4:25 PM |
Mark M. |
there's a "JavaDoc" link in the nav area on the right
|
Mark M. |
which leads to the JavaDocs
|
Mark M. |
RestAdapter is a Retrofit 1.x thing, so you won't see that in those JavaDocs
|
Mike F. |
OK, nice, thanks. For the API info, i.e. the format of the JSON from Stackoverflow, that would be in the developer area somewhere on the so site?
|
Mark M. |
yes
|
Mark M. |
http://api.stackexchange.com/ in this case
|
Mark M. |
now, documentation quality and availability is... variable
|
Mark M. |
ideally, all Android libraries have great documentation
|
Mark M. |
and, ideally, all Web services have great documentation
|
Mark M. |
ideally, I'd be less balding
|
Mike F. |
Great. I want to practice more with this so that I can get familiar with how to use Retrofit.
|
Mike F. |
me too!
|
Mike F. |
lastly, what are you plans for handling the transition to Kotlin in your book, over the longer term?
|
Mark M. |
clearly, as our heads illustrate, it's not an ideal world
|
Mark M. |
probably nothing until 2019 at the earliest
|
Mark M. |
even then, I expect that Kotlin will mostly be in separate books
|
Mark M. |
there is no way that I am rewriting a 4500-page book to be all Kotlin, for example
|
Mike F. |
it seems like it will be complicated as people doing Android will have to know both languages moving forward, as legacy code bases will need to be maintained.
|
Mark M. |
"have to know" is a bit of a strong term
|
Mark M. |
it would be helpful to know Kotlin
|
Mike F. |
Got it.
|
Mark M. |
but lots of Android developers are not in position to spend the time on that, or lack resources in their native languages, and such
|
Mark M. |
Java isn't going anywhere
|
Dec 13 | 4:30 PM |
Mark M. |
in iOS, Objective-C is a horrible language, which is why everybody ran to Swift
|
Mark M. |
I don't think that Java is considered nearly as bad as Objective-C
|
Mike F. |
interesting. So you see the transition for Android being different, maybe slower and will take longer?
|
Mark M. |
whereas with iOS, it's difficult to actually use Objective-C now (from what I can see), I expect that Kotlin and Java will be peers eventually, once Kotlin catches up
|
Mark M. |
I don't expect Java to evaporate, in part because Java is used outside of Android, and some of what we do is designed to run in those environments as well
|
Mark M. |
Retrofit itself, IIRC, does not require Android, for example
|
Mark M. |
I expect rapid uptake of Kotlin in wealthy nations by a lot of developers
|
Mike F. |
OK, thanks Mark. Gotta run. Thanks so much for the book, it is amazing. I am preparing for an interview, and it has been very helpful.
|
Mark M. |
you're very welcome!
|
Mark M. |
Steve: back to you!
|
Mark M. |
(Mike: and good luck in the interview!)
|
Mike F. |
thank you. :)
|
Steve S. |
regarding the large number of views, a lot of them are TextView's, which is why i was wondering about the data binding framework
|
Mark M. |
you're certainly welcome to use it
|
Mark M. |
I like it, personally
|
Mark M. |
particularly now that we're in a Jack-free world, and Java 8 works with the data binding framework
|
Steve S. |
but it sounds like one option would be to create my own class to hold the views in a structured way?
|
Dec 13 | 4:35 PM |
Mark M. |
your fields can be anywhere you want, relatively speaking
|
Mark M. |
if you want them directly on the activity or fragment, fine
|
Mark M. |
if you want to use a separate class -- akin to how we use the view-holder pattern in RecyclerView -- go right ahead
|
Steve S. |
from a software engineering perspective, do you have a preference for any of the options we discussed (or would that depend on the details of the specific use case)?
|
Mark M. |
well, again, I haven't really found the number of fields to be the limiting factor
|
Mark M. |
usually, I run out of screen space first
|
Mark M. |
but your app may not have that particular limitation
|
Steve S. |
sure
|
Steve S. |
we are using a tablet
|
Mark M. |
I don't know that I'd use the data binding framework just to get a code-generated class to hold widgets
|
Steve S. |
ok
|
Mark M. |
but, I'd use the data binding framework for other reasons (like, say, data binding)
|
Mark M. |
and I'm happy to get the code-generated class to hold widgets as a side benefit
|
Mark M. |
though usually with data binding you don't need to reference those widgets as much anyway, as data binding expressions handle that for you
|
Steve S. |
ok, no more questions today
|
Steve S. |
i saw that you're coming out with some material about Bluetooth in the next book update. i'm looking forward to that!
|
Dec 13 | 4:40 PM |
Mark M. |
it'll mostly be demonstrating https://github.com/IvBaranov/RxBluetooth
|
Mark M. |
with callouts to the actual Bluetooth API
|
Mark M. |
and I made a note to double-check that I'm closing my BluetoothSockets... :-)
|
Steve S. |
right!
|
Steve S. |
thanks for all the help!
|
Mark M. |
you're welcome!
|
Steve S. |
have a great rest of the day!
|
Mark M. |
you too!
|
Steve S. | has left the room |
Mark M. |
Mike: if you have additional questions, go right ahead
|
Dec 13 | 4:45 PM |
Mike F. |
just lurking. gonna sign off - see you another time. Thanks again.
|
Mark M. |
you're welcome!
|
Mike F. | has left the room |
Dec 13 | 5:00 PM |
Mark M. | turned off guest access |