Office Hours — Today, July 2

Thursday, June 27

Jul 2
7:15 PM
Mark M.
has entered the room
7:20 PM
Mark M.
turned on guest access
7:35 PM
ansh s.
has entered the room
Mark M.
hello, ansh!
how can I help you today?
ansh s.
greetings sir, how are you doing?
Mark M.
OK, and you?
ansh s.
i am good. got a couple of questions
first was regarding that mediator live data i asked via community forum
I had this query regarding filtering a livedata, like changing its source, and you suggested i should use mediator livedata. i looked into and was able to sucessfully implement that feature
Mark M.
cool!
7:40 PM
ansh s.
But here is a thing: mediator livedata can take many sources and does not have a getSource function. So when i apply mediatorObj.removeSource(<oldSource>) , i actually don't know what to pass as 'oldSource'
Mark M.
you have to track that on your own, in another field/property
ansh s.
so how to get old source it is observing?
oh, like a global variable?
well, that was what i applied too
Mark M.
no, in the same scope as the MediatorLiveData
so, if your MediatorLiveData is held by a ViewModel, the ViewModel would also hold onto the oldSource
https://issuetracker.google.com/issues/131738009 is where I asked for an improved API here
ansh s.
yes, that's where i have created a function for updating source, and there i am tracking the old source via a global private variable
Mark M.
the word "global" worries me :-)
keep it in the same scope as the MediatorLiveData itself
there, lastSave is your oldSource
I hold onto the MediatorLiveData (saveEvents) and the last source (lastSave) in the ViewModel (MainMotor)
ansh s.
well, i did just like that and am calling my oldSource as 'global', am i using the word wrong hehe
Mark M.
ah, OK
7:45 PM
ansh s.
okay, next doubt.. i can't seem to find much resources on Executor , and i have came across a piece of code , that looks extremely cool. woul like to know how is it working
would*
View paste
    // this is how you run a "data returning method"  asynchronously:
        // will query for data on a side thread, AND THEN return the results on main thread         
        // this.ioExecuterService = Executors.newSingleThreadExecutor();
        try {
            return ioExecuterService.submit(new Callable<Tea>() {
                @Override
                public Tea call() throws Exception {
                    return dao.getRandomTea();
                }
            }).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            return null;
        }
sorry the comments are by me for the 'dumb' me
Mark M.
I suspect that does not work the way that you are thinking
from the comments, I assume that you are looking to execute this code on the main application thread.
get() will block that thread
which means the background thread was pointless
ansh s.
the dao.getRandomTea() is a call to rooms dao... and it should be asynchronous... and when i click a button this returns the query immediately, so that's why i assumed that
oh, so basically my main thread is blocked?
Mark M.
yes
submit() returns a Future and has your dao.getRandomTea() call be executed on a background thread, which is fine
ansh s.
so this means that query is being executted on the main thread? then how come my room db allows it without getting blasted?
7:50 PM
Mark M.
the *query* itself is being executed on a background thread
you are blocking the main application thread by calling get() on the Future
get() is a blocking call, tying up the current thread until the background thread completes its work and updates the Future to reflect that
ansh s.
oh, so 2 threads are being used, and for the time till the background thread runs, my main thread is blocked?
Mark M.
correct
so, this gets you past Room's "do not do operations on the main application thread" error
ansh s.
wow, so second thread is really pointless, and i thought this code is cool
Mark M.
if it makes you feel better, lots of people come to the same conclusion
ansh s.
and this sure is going to give me an ANR if i perform a long running operation,right
Mark M.
so, you are far from alone
if the operation is long enough, yes
for shorter operations, your UI is frozen
that's why Room yells at you if you try doing database I/O on the main application thread
ansh s.
have you covered the executors in any book? i can't seem to find it as a solo topic
i checked busy coder
Mark M.
that is a Java thing
not an Android thing
ansh s.
oh okay
Mark M.
so, I use Executor in some places, but I do not explain its operation
ansh s.
next was regarding paging library
Mark M.
I have not worked with that in ~18 months, so my memory may be fuzzy
7:55 PM
ansh s.
i have checked their videos and many code samples, they seem to be showing a great way of loading data, and in their illustrations, they show gifs which indicate that when loading data via paging library, we won't even have to use some shimmer loading, because the ui is itself getting created if you handle the null cases correctly
i tried with a lot of sample projects with paging library, yet i can't seem to get that shimmer loading like animation
Mark M.
I do not know what you mean by "shimmer loading like animation"
ansh s.
i am sorry i am running out of english words, not my best language. its like that placeholder to actual data convertion, like you see when using picasso or glide library
Mark M.
OK
particularly with Room, the paging often is fast enough that it is difficult to see a RecyclerView row with your placeholder content
8:00 PM
Mark M.
so, personally, I have not worried about it much -- I make sure the placeholders work in testing
ansh s.
yes,exactly... the data is getting loaded fluently and all, but i can't see the animation that i expected.. i thought that it is sending a list of null values at first, nd quickly changing it to actual data values, triggering the code in my holder
i even tried with network as resource, yet nothing... infact this time the recycler view scrolling was not that fluent
because downloadng was from the network, so the whole process was taking some time
Mark M.
¯\_(ツ)_/¯
I have not used the paging stuff very much, and I was not worrying about this aspect of the behavior at that time
and, as I mentioned, it was a while ago when I last used it
so, I do not recall the exact behavior, in terms of when we get null values for our data
sorry!@
ansh s.
no worries ..i just like how glide/picasso update the placeholder to actual data... was wishing if their was a way to do the same for text data
ok next doubt
how can i test my job schedular is working?
8:05 PM
ansh s.
do i have to sit in front of whole day to look to the logs
in front of adb
Mark M.
there are two aspects to this: is the actual job getting invoked, and is the work being done by the job doing what it should
ideally, you should keep your JobService fairly thin, so you can test your actual work directly
in terms of confirming that the jobs themselves get invoked, on a manual basis, manually change the time in your device or emulator
ansh s.
actual job getting invoked i guess... my job is making a notification everytime it is called... nd its a periodic job, like running twice a day
oh okay
so this internally depends on system time... but isn't there is a thing with job like if it is scheduled for getting invoked every 30 minutes, it will get invoked anywhere between that time interval?
Mark M.
yes, in principle
from a battery standpoint, it is more efficient to wake up the device once and do a lot of work than it is to wake up the device lots of times and do a little work each time
so JobScheduler tries to shift jobs around to wake up the device fewer times
8:10 PM
ansh s.
yes.. so if i got a notification(i.e an indicator that my job worked) like now, and i changed my phone time to 29 minutes( if my job is scheduled to get invoked every 30 mins) , would i immediately get a new notification>
Anshul V.
has entered the room
Mark M.
perhaps not "immediately", but I would expect it to trigger more quickly
let me take a question from Anshul, and I'll return to you in a bit
Anshul: hi! how can I help you today?
ansh s.
Sure... hi anshul
Mark M.
Anshul: do you have a question?
Anshul V.
Hey everyone, I am working on re-architecting the app to make it multi-modular, and wanted to discuss the way in which we can achieve it. The requirement came when we decided to split the app into 3 different apps. I know there are plenty ideas floating around the web, I wanted your take on it, Mark.
8:15 PM
Anshul V.
Do you mention modularization somewhere in books? I may have missed it..
Mark M.
no, sorry
I talk some about modules in terms of app modules versus library modules, but I do not get into modularization strategies or anything like that
I have... unusual experiences in that area, so I do not feel comfortable trying to offer advice on the subject
the closer you get to "how do I organize this massive project?", the less likely it is that my experiences will line up with what ordinary developers encounter
if you have very specific questions, I can try to answer them, but that's about it
ansh s.
haha, you call that "alphabet soup" ;P
the whole mvc mvvm mvp thing
Mark M.
ansh: while GUI architecture is part of project organization, it's not strictly related to if and how you divide the app into modules, so Anshul's area of concern is a bit different
Anshul V.
okay, thanks! I am also doing this the first time, so unsure about so many things. No worries, carry on. We are aware of all these ideas like Clean Architecture, and SOLID principles, and as Ansh mentioned, the "alphabet soup", but I guess I won't know enough until I implement and mess it up..
8:20 PM
Mark M.
for all of those things, do bear in mind that times and tastes change
ansh s.
i have yet to explore all of those areas, hehe
Mark M.
so today's GUI architecture buzzwords may be tomorrow's "tech debt"
ansh s.
<can i post my last question?>
Mark M.
yes, it's a free for all now, here in the last minutes -- if either of you have questions, go ahead!
Anshul V.
Haha.. yep, that's what I am cleaning up right now, all my tickets are labelled "tech debt". More abstractions, more scalable, more testable sounds fun until you actually get down to the bare bones of what makes the app tick. And the biggest drawback is that user doesn't know the difference..
Go ahead, Ansh!
Mark M.
Anshul: agreed -- that's why I recommend that teams focus first on having a documented, consistent implementation, *then* worry about refactoring for those sorts of concerns
8:25 PM
Mark M.
the point behind all of these things is long-term maintainability, but having a documented, consistent implementation is far more important for maintainability than are the details of the implementation itself, IMHO
ansh s.
thankyou. Actually this is also more about general testing. I have to explore more of testing, but i can't understand how to test these background running tasks like work manager and jobschedular... the invoking part
like you said, your way was okay, and i can try, but is there some official or more framework like thing that i can use?
Mark M.
WorkManager has a dedicated testing artifact (androidx.work:work-testing), so I would check that out
in terms of automated testing, I focus more on the work being done and less on the system-supplied trigger
Anshul V.
Agreed! I am documenting as I go about refactoring. As always, thank you for your help, Mark!
Mark M.
Anshul: you're welcome!
ansh s.
oh okay. thanks sir
Mark M.
you're welcome, too! :-)
Anshul V.
has left the room
ansh s.
"And that's the wrap for today" hehe
ansh s.
has left the room
Mark M.
nope, we have 51 seconds
er, 46
no, now 38
:-)
8:30 PM
Mark M.
*now*, it's a wrap for today's chat
the next chat is tomorrow at 9am US Eastern
and the transcript of this chat will go up on https://commonsware.com/office-hours/ shortyl
er, shortly
have a pleasant day!
Mark M.
turned off guest access

Thursday, June 27

 

Office Hours

People in this transcript

  • ansh sachdeva
  • Anshul Vyas
  • Mark Murphy