Office Hours Transcript: 2021-12-23
how can I help you today?
I would like to know if my use of GlobalScope is warranted. Here is the use case:
I have an API-like class that provides the sunrise/solar noon/sunset/ times for any given day at any given location. Those values need to be available at any time, by any other component of the app.
The location is provided by the user directly or acquired by the GPS as a state flow, and if there is any change, the sunrise etc… values are automatically updated,using stateIn to make operations on StateFlows. Similary if the user changes the current timezone, etc…
A coroutine scope is required for stateIn, and now I’m using GlobalScope. Is that OK?
(hello, kafran – I will be with you shortly!)
john: personally, outside of book examples, I avoid
I’d rather create my own
CoroutineScope and use that, such as via dependency inversion
my Room examples use this approach, since
viewModelScope may have too short of a lifetime for write operations
using your own scope can help with testing and can help with edge cases (e.g., you need to clean up the old scope when the user logs out)
JetBrains also recommends avoiding
GlobalScope, in their documentation on
GlobalScope is unlikely to completely break your app, though
let me take a question from kafran, and I’ll be back to you in a bit for your next questino
@kafran: your turn! how can I help you today?
@kafran: if you come up with a question, chime in
in the meantime… @john: do you have another question?
Yes, I’m not clear about how to define my own coroutine. A context is required, but my class is not linked to any UI/activity or viewmodel. It needs to be available even when the user closes the app.
your process can go away milliseconds after your UI goes to the background
So, I’m trying to build that camera SDK. I’m using viewBinding and Gradle 7.0.4; It seems I’m facing problems when I try to use this SDK on a project which runs a Gradle version < 7; If my SDK is built using viewBiding, whoever uses it need to configure viewBinding = true on their Gradle as well? What should I keep in mind to get maximum compatibility?
kafran: sorry, what "that camera SDK" are you referring to?
A custom one. I talked to you last week ;P
john: and, overall, keeping your process running all the time isn’t free, which is why Google has a lot of restrictions on it
kafran: sorry, but I am a bit short on sleep – can you refresh my memory about what you are trying to do?
sure. but overall, are you ok?
um, other than not having gotten a good night’s sleep, yes – how are you?
I’m ok too =). I was not so good a few weeks ago, bad nights as well. Then I started to walk more and exercise gain and eat less junk food and I started to feel good again.
glad to hear it! getting back to your problem… is "that camera SDK" referring to the fork of CWAC-Cam2 0.6.9, or something else?
If my SDK is built using viewBiding, whoever uses it need to configure viewBinding = true on their Gradle as well?
I have never used view binding in a library. I would expect it to contain the generated code, though, so I would be surprised if it caused problems for consumers of the library.
Well, about the SDK. I’m trying to build a custom SDK that will handle the camera and send frames to the client API for validation. We have a custom view for the camera with a face shape on it, etc. When I was testing it I tried to import it on a "old" template and I was facing problems with Gradle version e viewBiding. On the SDK side, I’m using viewBinding to manipulate the view. Whoever uses this SDK gonna have to use viewBinding as well? Or at least enable it on Gradle? And If I use the last Gradle on my project, will I face compatibility problems with who uses an "old" gradle version?
@john: I would focus on what the minimum requirements are for your background work. For example, if it is something that can be done periodically by
WorkManager, I think
CoroutineWorker will give you a scope to use.
The fork of CWAC-Cam2 I’m using only as reference.
Whoever uses this SDK gonna have to use viewBinding as well? Or at least enable it on Gradle?
AFAIK, no, but, again, I have not tried this.
view binding is a code generator, and I would hope that your library contains the generated code for its own layouts
Probably my problem faces on gradle configuration =/; I always get confused about these commands on dependencies (implementation, compileOnly, etc)
Do I need to config something on Gradle so my library exports the generated code?
Yeh, the problem is with the code generator. When I use my library on a project without viewBinding on Gradle and with Gradle 4.0.1 it gives an error saying it could not find the viewBinding stuff
do you have the exact error available?
But if I update to gradle 7 it works
@john: in the end, there is nothing stopping you from using a foreground service to keep your process running for a long time, but Doze mode, app standby, and manufacturer-specific power management logic may interfere with what you are trying to do
@Mark_Murphy let me run it again to get the exact error
@Mark: you are remarkably amicable for someone who didn’t get a good night of sleep.
If you will, the class is something akin to LocalTime, where LocalTime.now() gives you the current time anywhere in your app.
It doesn’t need to be running all the time, but for example if I want to send a reminder at sunrise everyday, I need to calculate the next day’s sunrise time at the same time the reminder is sent to the BroadCast receiver by AlarmManager. Also, if the current location is changed (GPS update for e.g.), the reminder time needs to be updated automatically.
Would something like this work?:
fun provideCoroutineScope(app:Application) = CoroutineScope(app as CoroutineContext)
app as CoroutineContext seems wrong –
CoroutineContext is unrelated to Android’s concept of
if you look at my Room samples, IIRC I use
IIRC I have a chapter on custom scopes in Elements of Kotlin Coroutines
Alright. I will try using CoroutineScope(SupervisorJob()) for now and have a look a the chapter.
I will investigate this again. I’m getting another error now xD
Hey Mark. I hope you sleep well tonight. =)
thanks! happy holidays!
You gonna be here on 26, 28 and 30?
It is the last office hours, right?
yes, those are the last three office hours chats, and I should be here for those
Nice. I will show up, at least to chitchat on the last office hours xD
Regarding, Android contexts, I understand that you’re not supposed to pass them to ViewModels, but I find myself passing the Application defined in the Hilt Module to do things like defining receivers, converting resource integers to strings…
Mark, do you work as freelancer or do you have full time job?
Is there a way I can avoid that or is it OK?
Application is a safe
Context for viewmodels to use – that is why the Jetpack has
@kafran: I used to freelance, but now I am an employee with a regular job
@Mark: if you pass application, do you need to use AndroidViewModel instead of ViewModel? (I’m asking because I’m oassing Application to a normal viewmodel now, do I need to to change that?)
@Mark_Murphy I’m an employee as well, but I’m not happy with it =/; can we talk about freelancing and entrepreneurship? I would like to try something like this but I don’t even know how to start, I don’t even know how I would structure and charge for a project =(
if you pass application, do you need to use AndroidViewModel instead of ViewModel?
So long as the
Application is getting there, you should be OK.
AndroidViewModel is simply an "out of the box" solution that does not require a custom factory or a DI framework.
I would like to start freelancing as a second source of income
@kafran: did you sign any sort of agreement with your employer that might limit your ability to freelance?
great! that can be a major source of difficulty for people
I suppose I should clarify an earlier statement of mine: I used to be a contractor, and now I am a regular employee
I have never held a full-time job and did paid work on the side
@Mark I’m starting my company next year, I’ll hire you to sleep.
My major source of difficulty is to start. To get clients, organize a project, how to charge and give price… the entrepreneurship part of the whole. I’m used to just focusing on one thing.
@john lol, nice.
that’s all kinda outside the scope of what I can really help with – I’m good at Android app development and not so good at, say, marketing
you might see if there are part-time contracts available to developers in your area
remote work has greatly expanded the range of where you can do work, though there usually are still time zone limits
yes, thas what I’m looking for
Well, thats all for today. See you on 26. Go sleep (=
@Kafran you might want to check Phillip Lackner on youtube
He has a tips a freelancing
@john yep, I’m following him
@Mark thank you for your help as always.