Office Hours Transcript: 2021-07-17

sudokai joined

hello, @sudokai!

 

how can I help you today?

Hi Mark

 

How do you lazy load custom views?

that is difficult to answer in the abstract

I have this screen that has tons of views and when navigating to it, you can feel the choppiness

 

So I thought of lazing loading the views below the fold

having tons of views may be the problem, more so than some of those views being custom

I found this thing called ViewStub

 

But you need to pass a layout to it

 

You cannot pass your custom view to it

no, but it could point to a layout resource that references custom views

 

personally, I have not used ViewStub, so there may be limitations that it has that I am unaware of

 

frequently, in places where I might have tons of views, there is enough duplication of those views that RecyclerView becomes a reasonable host

no, but it could point to a layout resource that references custom views

Nice, it didn’t occur to me before

and, more generally, choppiness may be being caused by other issues, such as doing I/O on the main application thread – I might use profiling to try to determine where the time is going

RecyclerView becomes a reasonable host

It’s not a list of stuff, it’s just a very long screen with lots of views

 

I might use profiling to try to determine where the time is going

I did and I saw a lot of time, like 40% of the time spent in ContraintLayout onMeasure

IvanoNoSpaces_reload joined

hello mark && sudokai

bear in mind that RecyclerView supports a heterogeneous roster of views – so "a very long screen with lots of views" might still benefit, even if there is limited actual recycling

 

(hello, Ivano – I will be with you shortly!)

 

the challenge with ViewStub becomes: when do you tell it to inflate()?

 

it can’t be right away, as all you do is make your performance problem worse

 

and if it is after some amount of scrolling, figuring out when "some amount" has occurred may or may not be easy

 

some developers use libraries like Epoxy and have every screen be handled by a RecyclerView

 

personally, I hated my limited Epoxy experience and would not recommend it as a general tool, but that sort of approach may be appropriate for certain screens

 

going back to the source of your pain (onMeasure() of ConstraintLayout), how dynamic is your content? are you forcing a ConstraintLayout to go through layout a lot?

IvanoNoSpaces_reload left

 

IvanoNoSpaces_reload joined

@sudokai: how about I return to you in a bit and take a question from Ivano in the meantime

 

@IvanoNoSpaces_reload: hi! how can I help you today?

Okay

Hi Mark, I do not have really specific questions, I am checking my last finding in the new stack i am busy to kind of find some question I may ask you, is that ok with you?

sorry, but I am having difficulty understanding your question

I mean I am looking for a question to ask you

OK… ping me when you have something!

probably I have none, because I am becoming familiar with the code I am presently dealing with.

 

OK thanks

in the meantime… @sudokai: back to you!

how dynamic is your content?

 

it’s not dynamic at all

 

scrolling, figuring out when "some amount" has occurred may or may not be easy
You can use a scroll listener no?

yes, but how far is far enough? phones and tablets vary in height, density, font scale, etc.

 

you mentioned custom views – one test to run is to "dumb down" those custom views as much as possible, or swap in something really lightweight as a experiment

 

if the jank goes away, then you know that the problem lies somewhere in the implementation of the custom views

 

if the jank persists, then perhaps it truly is just the number of views

IvanoNoSpaces_reload left

 

IvanoNoSpaces_reload joined

 

IvanoNoSpaces_reload left

 

IvanoNoSpaces_reload joined

 

IvanoNoSpaces_reload left

 

IvanoNoSpaces_reload joined

(1VAN0 raises hand.looper)

@IvanoNoSpaces_reload: if that means you have a question, go ahead!

In the previous job i messed in production causing various bugs implementing a no internet connection check

 

now i should implement again a screen that in the case there is not internet connection, shows a static screen indicating "no internet connection" and a retry button

 

on the internet i found different solutions

@Mark_Murphy It must be the map, one of the views is a Google Maps view

@sudokai: a Google Map is going to be fairly heavyweight, so it causing jank is not surprising

  1. a work manager. 2) using a lifecycle and link to the Application and check in on create if there is internet connection 3) some callbacks that signal is there is not internet connection via a broadcaster

sudokai left

 

sudokai joined

I would like to know if you reckon that for a simple application what of the three approaches above indicated (or maybe a fourth one) would prevent problems/bugs. In other words in 2021 do you have an opinion about the safest/simplest approach to manage a no internet connection?

Ivano: personally, I find "no Internet connection" logic tends to be bad, as it is based on bad assumptions. There really is no such thing as an "Internet connection" that we can detect. IMHO, the only thing that matters is whether we can connect to a desired server at the desired time.

I see your point

For example, there is code floating around showing using ConnectivityManager to determine if we have an "Internet connection". That at best shows that we have a network connection. Whether the network is any good is another matter – if we have a network connection, but it cannot get to a server that we want to talk to, that connection is useless.

yes I agree, and i used in my previous job this connectivityManager linked to a workmanager and caused 200 crashes

 

in production

 

So i have the feeling that you would manage this issue, launching a screen on a specific 500 response?

 

that could mean, kind of no internet connection

yes, or a timeout or other error coming from the code that is attempting to get data from the Internet

 

after all, accessing the Internet may be fine, but we might not be able to reach the server (server crashed, DNS problems, server runs on AWS and AWS is having a bad day, etc.)

but different is the use case that the user does not have activated the wifi, if requested, or is coming back from a flight and forgot to set airplane mode off

if you want to detect airplane mode specifically, you’re welcome to do so – I think we can find out from Settings.System

yes I did in the past

though then you get cases where the person has airplane mode on but WiFi also turned on (e.g., in-flight WiFi)

is a peace of cake

 

yes

occasionally, it may be worthwhile to try tackling those cases – for example, in an IoT scenario, if the phone is on mobile data and not WiFi, perhaps you know that you will not be able to communicate with the desired IoT device

yes looks logical

 

thanks

if either of you have any other questions, go right ahead!

well you spoke with sudokai about recycler view and epoxy

 

is reasonable thinking that in one year will be a problem only of legacy code

 

and that compose is going to substitute recycler views?

Compose UI is definitely going to be popular

I guess so but better to ask you because you see the things from a different point of view

I would be hesitant to describe existing views as "legacy code" only one year from now

 

that would be like considering Java to be "legacy code" in, say, 2019

nice thank you to share your qualitative opinions, it really counts to me

Compose will have an adoption curve, like anything else

 

it is more difficult to migrate from views -> Compose than it is to migrate from Java -> Kotlin, just because there is no automated tool to help with that migration

 

by 2025, I expect Compose UI to be the default choice… but not by mid-2022

well i am glad that knowing constraint and relative/linear/frame/coordinator layouts is a valuable skill to have after all the effort i put to learn them

 

but mark

 

please as you are in prophecy mode

i really, really would love to know if you could say one year we will start to code ios and android as new normal

 

ROTFL

 

(i mean togheter with kotlin multiplatform)

Compose may help some with Kotlin/Multiplatform adoption, but that too will have a slow adoption rate

 

and there are library ALREADY that translate swift UI in compose

 

anyway time is off thanks

yeah, that’s a wrap for today’s chat

 

the next one is Tuesday, in the 7:30pm US Eastern slot

 

have a pleasant evening!

have a pleasant morning Mark and sudokai