Mark M. | has entered the room |
Mark M. | turned on guest access |
May 12 | 4:00 PM |
Suleyman O. | has entered the room |
Mark M. |
hello, Suleyman!
|
Mark M. |
how can I help you today?
|
Suleyman O. |
Hi Mark!
|
Suleyman O. |
hope your day is going great :) I have a question about ViewModel Factories, I'll paste it in
|
Suleyman O. |
I’m not sure when to use a ViewModelFactory. I thought it was for cases when you need to pass parameters to a ViewModel. But, for example I have a Repository class that has some query methods that take in some parameters and fetch data from DB. In my ViewModel constructor I initialise an instance of Repository and then in getters and setters of the ViewModel I can just use mRepository.get(parameters). So it can be done without the Factory, so what is it for then? :) To avoid leaks?
|
Mark M. |
it is for the situation that you mentioned: when you need to pass parameters to a ViewModel
|
Mark M. |
for example, you might want your ViewModel to also handle the saved instance state Bundle, so configuration is in one spot
|
Mark M. |
if you do all of the initialization of the ViewModel in its constructor, you would need the Bundle in the constructor
|
Mark M. |
and, for that, you need a custom factory
|
Mark M. |
if you do not need to pass data into the ViewModel constructor (beyond the Application for an AndroidViewModel), you may not need a factory
|
Mark M. |
though there may be scenarios where you wind up using one as part of some other framework (e.g., dependency injection)
|
May 12 | 4:05 PM |
Suleyman O. |
Thanks for the explanation! So in my case, I believe I don't need it because I only need parameters for my queries.
|
Mark M. |
I certainly would not create one just for giggles :-)
|
Suleyman O. |
I wouldn't either :D thanks!
|
Mark M. |
you're welcome!
|
Suleyman O. |
So it's for more complex cases, for example when a repository handles whether to fetch data locally or from a network and therefore needs some parameters to work.
|
Mark M. |
that's one possibility
|
Mark M. |
or, you might use different factories for test scenarios, where you mock at the ViewModel level
|
Suleyman O. |
oh yeah, that's true, didn't think of that scenario :)
|
May 12 | 4:10 PM |
Mark M. |
it's a nice hook to have at your disposal, and may be more useful in more complex projects
|
Suleyman O. |
can I ask you one more question?
|
Mark M. |
go right ahead!
|
Suleyman O. | |
Mark M. |
what onChange() method are you referring to?
|
Suleyman O. |
I'm sorry, I meant onChanged method of the Observer
|
Mark M. |
this is a LiveData Observer?
|
Suleyman O. |
exactly
|
Mark M. |
I don't see where that would necessarily create a leak
|
Mark M. |
the actual Observer itself (anon inner class or lambda expression) might be a leak, depending on what it references directly
|
Mark M. |
basically, don't reference views directly in the observer
|
May 12 | 4:15 PM |
Mark M. |
though, even there, since the LiveData is lifecycle-aware, you're probably going to be OK
|
Mark M. |
I'm not ruling out the possibility of a leak in your scenario, but I cannot picture what would be leaked... but I'm short on sleep today, so my brain is fuzzy
|
Suleyman O. |
I understand, I have the same problem :) thanks for the explanation, it's very helpful :)
|
Suleyman O. |
I won't torture you anymore, but I'll come back next time :D
|
Mark M. |
trust me, this isn't torture
|
May 12 | 4:20 PM |
Suleyman O. |
Then I have just one last question if you don't mind :D
|
Mark M. |
go ahead!
|
Mark M. |
note that if there are more people in the chat room, I would rotate between everyone, but you are the only one here, so the floor is yours
|
Suleyman O. |
I fully understand that :)
|
Suleyman O. |
I have a question that have been asked a million times maybe, but still bothers me. Providing different layouts for different screens, is it sufficient to provide the layouts based on smallest width (e.g. layout-sw600dp)?
|
Mark M. |
that is difficult to answer in the abstract
|
Mark M. |
I would say that typically -swNNNdp is sufficient
|
Mark M. |
but you might also need -rtl, if the default RTL layout mirroring is not working to your satisfaction
|
Mark M. |
if the same app supports unusual device types -- such as Android TV -- you might also need other resource set qualifiers to help distinguish them
|
Mark M. |
so, for example, I am experimenting with the new Chrome OS emulator image right now
|
Mark M. |
it's possible, though probably not ideal, that you would want different layouts for Chrome OS, to take into account keyboards and mice
|
Mark M. |
in which case, I think -pc would be the qualifier
|
May 12 | 4:25 PM |
Mark M. |
in contrast, having density qualifiers (e.g., -xhdpi) or language qualifiers (e.g., -kr) would be code smells IMHO
|
Suleyman O. |
yeah, you are right, that what I was suspicious about, providing layouts based on density (hdpi, xhdpi, etc.)
|
Mark M. |
density as a resource set qualifier behaves odds
|
Mark M. |
er, behaves oddly
|
Mark M. |
density is designed for drawable and mipmap, where Android can convert between densities automatically via downsampling and upsampling
|
Mark M. |
but the resource system treats it as though *any* resource can be converted between densities, somehow
|
Suleyman O. |
oh ok got it, so it's not very reliable for layouts
|
Mark M. |
as a result, any lower-priority qualifier, like API level (-v21), gets ignored
|
Mark M. |
right
|
Mark M. |
usually, drawables/mipmaps are the only resource types to use densities
|
Mark M. |
*maybe* values/, if for some strange reason you don't like the normal dimension unit calculations
|
Mark M. |
anything else is unlikely to be what you want
|
May 12 | 4:30 PM |
Suleyman O. |
ok perfect, the questions that were bugging for a while are now answered! Thanks a lot! And thanks for providing such a service, it's great to have someone as knowledgeable providing advice :)
|
Mark M. |
I am happy to be useful!
|
Suleyman O. |
Thanks again :) Have a great day and try to get some sleep! :D
|
Mark M. |
thanks!
|
Suleyman O. |
Bye!
|
May 12 | 4:50 PM |
Suleyman O. | has left the room |
May 12 | 5:00 PM |
Mark M. | turned off guest access |