Full Guide to Dependency Scopes with Dagger-Hilt - Android Studio Tutorial

Поділитися
Вставка
  • Опубліковано 29 вер 2024

КОМЕНТАРІ • 53

  • @dahlola
    @dahlola 2 роки тому +1

    Nice little tutorial! Noticed that you seem to use the Xcode-Dark theme, I really like that theme :)

  • @AmirrezaLaghaee
    @AmirrezaLaghaee 2 роки тому +1

    Your tips and trainings are just extraordinary, man! thank you

  • @stoyan_vuchev
    @stoyan_vuchev 2 роки тому +8

    Hey Philipp 👋 I'm learning something new from You everyday. With proficient and detailed explanation I get a clearer picture in my head. 💯🔝💪

  • @twd97
    @twd97 2 роки тому

    I hope you add subtitles for Indonesian, I am greatly helped from your Android learning video, thank you Mr Philipp Lackner. 🙇

    • @shidqi100
      @shidqi100 Рік тому

      I bet you can improve your english a lot just by watching english youtube videos without subs, that's how I learned english.

  • @mustafaammar551
    @mustafaammar551 2 роки тому

    very cool video
    thank you bro👍👍👍👍

  • @coffeedude
    @coffeedude Рік тому

    What theme are you using? It looks pretty

  • @masmmaw
    @masmmaw 2 роки тому

    should i register my use_case class as ViewModelComponent instead of SingletonComponent considering that most of the use_case class is in charge of fetching data from repository and partly managing flow for logic to ui?

  • @aabhishek4911
    @aabhishek4911 2 роки тому

    Scoping viewmodel in compose is so ugly , fragments with individual composable ftw

    • @danjanuspineda4330
      @danjanuspineda4330 2 роки тому

      you mean sharedViewModel in compose, but yeah cant use that

  • @shlusiak
    @shlusiak 2 роки тому +15

    Why do you scope the dependency at all? It makes no difference in your examples, which are more about scoping the viewModels to the right anchor, but since there is only a single injection point and you are not consuming the Activity or Fragment as a dependency in your SessionTimer, scoping it to the ViewModel is misleading in my mind. The ViewModel is already retained by Android, so by using @ViewModelScoped you hold a reference to SessionTimer twice, but both with the same scope. Under the hood, scoping it will use a Lazy instance, which is only useful if the same instance needs to be used in multiple injection points. Dependencies are also never destroyed, they just fall out of scope when there are no more references to it and are then garbage collected. If in doubt and unless specifically required, unscoped dependencies are usually better. I agree you don't want to use @Singleton here at all.
    This video is about scoping ViewModels and IMO does not sufficiently explain Dagger+Hilt scopes and when to use them.

    • @aabhishek4911
      @aabhishek4911 2 роки тому +1

      Yes , it was more about sharing viewmodels across fragments / composables without the viewmodels themselves having lifecycle of activity .

  • @raheemadamboev
    @raheemadamboev 2 роки тому +1

    also singleton timer can be victim to race conditions

  • @ubersticks
    @ubersticks 2 роки тому +2

    Hey Philip - thanks again for your videos. I enjoy your full projects where you demonstrate your own "best-practices". I can see these have changed a bit over time as you refine your techniques and new features appear. I would like to see a project that updates your usages (or non-usage) of MVVM, @Singleton injection, single ViewModel State class vs separate state val/vars, SharedStateFlow (and other topics I am missing here) where your practice has been refined over the past year. I like to use your projects as a template for things that I do on my own.

  • @ЗелимханМагомадов-э2н

    Thanks bro! You helped me a lot!

  • @ryanw-youtube
    @ryanw-youtube 2 роки тому +1

    A pity you didn't post this video a few months earlier because otherwise I shouldn't have to explain this by myself, saying we shouldn't abuse Singleton, which people at that time didn't agree with me, but they watch your video and they trust you :)

  • @tyablix
    @tyablix 2 роки тому +3

    Great video :). Do you have a recommendation for using Retrofit with multiple base URLs? With Hilt in particular one option could be to provide a new retrofit instance for each API interface that uses a different URL. However, I'm not sure if this is the best approach.

    • @EnelAlmonte
      @EnelAlmonte 2 роки тому

      I would do the same as you. I think there's no better way

    • @PhilippLackner
      @PhilippLackner  2 роки тому +1

      If you know the base URL at compile time, you can just pass the right one into the interface (like localhost + prod). If not, you could replace it using an interceptor

    • @Daaaaaaavid
      @Daaaaaaavid 2 роки тому

      Hmm, then why you simply don't use the BASE_URL and put the complete URL directly instead?

    • @moheltanani1036
      @moheltanani1036 2 роки тому

      I logged in to ask the same question but saw that you already asked it. I’ll look up how to do it using hilt.

    • @tyablix
      @tyablix 2 роки тому

      In my case I do know the URLs at compile time. Do you mean like this?
      interface ApiOne {
      @GET("aPath/")
      suspend fun getSomthing(
      @Path("thing_id") thingID: String,
      ): ThingDto
      companion object {
      const val BASE_URL = "URLONE"
      }
      }
      interface ApiTwo {
      @GET("anotherPath/")
      suspend fun getSomthing(
      @Path("another_thing_id") anotherthingID: String,
      ): AnotherThingDto
      companion object {
      const val BASE_URL = "URLTWO"
      }
      }
      @Provides
      @Singleton
      fun provideApiOne(): ApiOne {
      return Retrofit.Builder()
      .baseUrl(ApiOne.BASE_URL)
      .addConverterFactory(MoshiConverterFactory.create())
      .build()
      .create()
      }
      @Provides
      @Singleton
      fun provideApiTwo(): ApiTwo {
      return Retrofit.Builder()
      .baseUrl(ApiTwo.BASE_URL)
      .addConverterFactory(MoshiConverterFactory.create())
      .build()
      .create()
      }

  • @Daaaaaaavid
    @Daaaaaaavid 2 роки тому +1

    DI is to me the most uggly part of modern programming, I really hate it :D

    • @EnelAlmonte
      @EnelAlmonte 2 роки тому +1

      You need to create a big project without DI, and later you will love DI. It solves problems that you don't know you had.

    • @Daaaaaaavid
      @Daaaaaaavid 2 роки тому

      @@EnelAlmonte most of the android devs never saw a project as big as a few I worked on for years, never missed DI

  • @Ben-wx1ln
    @Ben-wx1ln 2 роки тому +1

    As a professional dev, thanks, that's really cool. The part scope vm to nav scope. I spent a lot of time to figure it out.

  • @TVHovna
    @TVHovna Рік тому

    Sorry, but even the Dagger documentation states that you should not really bother with smaller scopes until your application's logic requires it (like it does in this video). The example you've given here is great, but it does not work to say in general that you should always use the smallest scope available/suitable.

  • @duongvuthe7765
    @duongvuthe7765 Рік тому

    how you know this knowledge, where is place that you find document?

  • @renzcarlosalanga6077
    @renzcarlosalanga6077 Рік тому

    Does singleton in dagger survive process kill?

  • @chethanbshetty7998
    @chethanbshetty7998 2 роки тому

    Tbh, why are you creating component for your own class, you can directly use constructor injection. It does not make sense in this example.

  • @yeyaanshkit
    @yeyaanshkit 2 роки тому

    Please create a whole new series of Android Development (Kotlin) from Beginner to Advance Level..
    Please... Love you from India🥰🥰🥰

  • @kamanchomorgan9655
    @kamanchomorgan9655 2 роки тому

    Pleased make video about bottom navigation with compose destinations

  • @tmjromao
    @tmjromao 2 роки тому

    Hello Philippe, many thanks for the video. Could you please do a video explaining how to load json data from resources? Point the direction? Trying Gson library (deprecated). Now Moshi lib, but the examples don't contemplate local resources files. Loading json file for mock data is very common (easy) in ios development.

  • @rajiac7552
    @rajiac7552 2 роки тому

    Exactly the video I was looking for 🙏

  • @abdelrahmanesam3684
    @abdelrahmanesam3684 2 роки тому

    very awesome man ..... I have a question please ..... I am trying to access my storage by media store .... this needs the context but I think this is a logic and I want to put this fun in my viewModel ....I have tried to inject the instance of this object to the viewModel by hilt but it doesn't work so I have used androidViewModel .....can I Inject it or I am right to use androidViewModel or just let this fun in composable ?

  • @sureshsivalingam01
    @sureshsivalingam01 2 роки тому

    Is it correct scoping our repository to singleton? In MVVM Clean Architecture

  • @dev_jeongdaeri
    @dev_jeongdaeri 2 роки тому

    Super amazing!😎👍

  • @aravindsundar1955
    @aravindsundar1955 2 роки тому

    Could you create a video on how to use compose and XML based views like dialogs, fragments?

  • @muhannddh
    @muhannddh 2 роки тому

    What is the safe way to collect flows, in case the app goes to the background, can you make a tutorial on this topic?

  • @user-rz1hv
    @user-rz1hv 2 роки тому

    why do your dagger-hilt dependencies differ from those in official dagger hilt site and android docs guide?

  • @Hooperlink
    @Hooperlink 2 роки тому

    thx a lot for what you r doing! keep it going pls!

  • @WaheedKhAn-mw8fo
    @WaheedKhAn-mw8fo 2 роки тому

    Such a nice explanation (Y)

  • @immortal_lnight
    @immortal_lnight 2 роки тому

    First

  • @masmmaw
    @masmmaw 2 роки тому

    Should we write the @AndroidEntryPoint annotation on all activities or fragments that we have or only on the activity that is the launcher?

    • @Daaaaaaavid
      @Daaaaaaavid 2 роки тому +2

      Please take Android's Hilt Codelabs, you don't seem to understand the basics yet, you only use that annotation in the classes you need DI.