How to use Dependency Injection in SwiftUI | Advanced Learning #16

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 พ.ย. 2024

ความคิดเห็น • 87

  • @TechnoBog-ov2mp
    @TechnoBog-ov2mp 3 ปีที่แล้ว +20

    I found this channel as the best for iOS developers

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +2

      Thanks TechnoBog! Tell your fellow devs 😎

  • @allisondelta1
    @allisondelta1 2 ปีที่แล้ว +4

    Blown Away ! Your tutorials are so rich with information. I have adapted my own logic based on your practices. Very succinct ! TY !

  • @priceringo1756
    @priceringo1756 3 ปีที่แล้ว +3

    Great post! About two thirds through the video I am on the edge of my seat thinking what about testing? Please don't wait to talk about that. You came through in the clutch. I realize you are going to expand on that in the future. Don't change a thing.

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +5

      Unit Testing video is scheduled for this Monday and UI Testing is after that 🥳

  • @candylover2456
    @candylover2456 2 ปีที่แล้ว +11

    Amazing! You never fail to teach a hard concept and make it easy. Thank you so much, you make a huge impact with your work :)

  • @bbongrip
    @bbongrip 3 ปีที่แล้ว +6

    So awesome! I really appreciate the way you explain things. Been looking for a while for videos that go in depth and really explain things instead of just showing one example. Keep up the good work!

  • @spradohak
    @spradohak 11 หลายเดือนก่อน +1

    Brilliant, I had to watch it multiple times but now it make more sense.
    Thank you Nick

  • @TheMadG007
    @TheMadG007 2 ปีที่แล้ว +3

    Thank you very much, the entire last week I tried to learn how to replace my singleton Storageprovider (for Core Data) in an appropriate way with dependency injection for easier testing .... I struggled especially regarding initialization of the ViewModel (as StateObject)... everything I needed to know was in this video! 👍

    • @SwiftfulThinking
      @SwiftfulThinking  2 ปีที่แล้ว +2

      Hah that’s so awesome to hear! Thanks for the comment

  • @pranavbellamprasad6314
    @pranavbellamprasad6314 5 หลายเดือนก่อน

    The best video out there to understand about Dependency Injection. Thanks!

  • @BSharing09
    @BSharing09 ปีที่แล้ว +1

    Excellence tutorials, you are the best teacher.

  • @Денис-ж3ф5р
    @Денис-ж3ф5р 2 ปีที่แล้ว +1

    What a minute how to inject a view model to a view, that's why I started watching the whole video. Good stuff BTW.

  • @AdjieSatryo
    @AdjieSatryo ปีที่แล้ว +2

    So easy to understand explanation. But the main reason im using singeleton is to using it from any class (ie. Network Manager, Socket Manager, Coredata manager). So, how can i share dataService class to other class? Thank you

  • @jimmyhuang1114
    @jimmyhuang1114 2 ปีที่แล้ว +1

    Wonderful tutorial about dependency injection. Thanks you for your efforts and clear demonstrations. :)

  • @zawysa
    @zawysa ปีที่แล้ว +2

    In general good work. Though you forget to mention that dependency injection can be very difficult to keep track of in big projects. Keeping track of all the dependencies is like visually having to keep track of a big DAG in your head. Do you know good DI container frameworks?

  • @samisanoof
    @samisanoof 3 ปีที่แล้ว +3

    I found your way of explaining things the best of all. Do you have any plans to make similar videos on Combine Framework?

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +7

      Hey Sami! I did some beginner level Combine videos in the "Continued Learning" playlist and I'll be doing some more advanced ones later in this playlist. I don't have a separate playlist just for Combine though 😅

  • @950djb
    @950djb 9 หลายเดือนก่อน +1

    What if you have a network service that is also an ObservableObject? It maintains properties such as a @Published accessToken and other things like that. What are your thoughts on nested @StateObjects and DI? I've found that injection an ObservableObject into another ObservableObject can get kind of wonky.

  • @tomlester8681
    @tomlester8681 หลายเดือนก่อน

    Great video. Are you effectively coupling service to the view model by making the view model responsible for sinking? I think it would be cleaner if the DataService just returned the data and was not part of the combined system.

    • @SwiftfulThinking
      @SwiftfulThinking  หลายเดือนก่อน

      Yes, for sure. You could inject a DataServiceProtocol rather than the whole DataService to avoid that too

  • @michaelsamoylov
    @michaelsamoylov ปีที่แล้ว

    Thanks for sharing this. I'm a big fan on Uncle Bob and Clean Architecture. It appears to be working well with one view but how would you make it work with several views sharing the same data service?
    For example, adding a record in one view and getting it in another view.
    Using `PassthroughSubject` from Combine and adding a data publisher looks like huge added complexity IMO.

  • @dispatchswift
    @dispatchswift 2 ปีที่แล้ว

    Superb explanations.

  • @coldy5845
    @coldy5845 4 หลายเดือนก่อน

    I have one question. How could we unwrap the URL without force unwrapping in this case without altering the syntax? let url: URL = URL(string: "...") ?? nil for example? but this seems not to be a soloution because value of type url can't be nil.. Thanks!

  • @光莒
    @光莒 2 ปีที่แล้ว

    It's great video!!! amazing~~~
    thank you for sharing this topic!!

  • @lincolndickerson1293
    @lincolndickerson1293 2 ปีที่แล้ว +1

    Great video again! One question and this may take another boot camp to answer, lol. I see core data as a data service. It is common practice to use @Environment to hold the context of the core data entity. It is then easy to swap contexts for previews, testing… Could we and should we put our DataService master class, the one that holds many data services, into the Environment and use it in the same way? Like core data we would inject into the View init fetching criteria.

    • @monstrositylabs
      @monstrositylabs 2 ปีที่แล้ว

      It's a question I would like to know as well. The problem I have found is that the Environment is not accessible from the ViewModel without a whole load of mess.

  • @josemanuelortizsanchez4983
    @josemanuelortizsanchez4983 ปีที่แล้ว

    Thanks Nick, good resume!

  • @arkemal
    @arkemal ปีที่แล้ว

    Man, this is an excellent course. Thank you!
    Do you have other video related to SOLID principales?

  • @TheSpiralnotizblock
    @TheSpiralnotizblock 2 ปีที่แล้ว

    What we would do if we have a mock class that conforms to a DataService protocol, but the mock class doesn't need all of these methods of the protocol? For example it needs only 2 of 5

  • @fsdolphin
    @fsdolphin 2 ปีที่แล้ว

    You're simply the best!

  • @kerryferguson8267
    @kerryferguson8267 3 ปีที่แล้ว

    Unrelated to the topic but I'm also learning great explanation skills and methods by watching your videos. I'm gonna steal it for my interviews lol.

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +2

      Haha that's great to hear. Being able to explain these topics out loud is a huge indicator of competence. I've interviewed many candidates that put topics like this on their resume but can't explain it in the actual interview 😔

  • @woozoobro
    @woozoobro ปีที่แล้ว

    You are truly amazing!!!! Now I understand your crypto app code!!!

  • @joe0212
    @joe0212 2 ปีที่แล้ว

    Should the view not be passed the view model as dependency

  • @aybarsacar9375
    @aybarsacar9375 2 ปีที่แล้ว +1

    Hi Nick, great content! -- it would be great to see an app from you with Clean Architecture from Uncle Bob using SwiftUI - it would be great to see the ways we can create dependency injection containers to manage them in our apps. Looking forwards to your new content

  • @mylaluna
    @mylaluna ปีที่แล้ว +1

    Hi man, your videos have been very helpful to me.

  • @stevexphotography128
    @stevexphotography128 2 ปีที่แล้ว +1

    Hi Nick! Thank you for your awesome tutorial! I'm wondering that what if I want to create more DataServices for other Models, say UserDataService for Users, CommentDataService for Comments, etc. There might be different parameters for the same func name `getData()` or `addData()` or something else in different DataServices. I've tried to add the same func name with no parameters in DataServiceProtocol but got errors, swift think they are different functions. So how should I arrange my DataServiceProtocol or should I still use protocol? I'm still a beginner and I would really appreciate it if you could offer some suggestions.(Maybe I should move this comment to the protocol tutorial...😂)

  • @amirpirzad2750
    @amirpirzad2750 2 ปีที่แล้ว

    So awesome 👏🏾🌹

  • @bonifacy9511
    @bonifacy9511 ปีที่แล้ว +1

    @Swiftful Thinking, how about passing VM's through .environmentObject? Or can you show how to initialize VM's on the top and pass it to he n child view? eg with this Dependencies class?
    We need a better explanation of that

    • @danielwatson6529
      @danielwatson6529 11 หลายเดือนก่อน

      what id been doing is creating the object in a parent, initialising the child vm in the view initialiser and passing the object in, simultaneously i passed down the object into the childs view. this way navigation could work and i read from the environment. but to interact with the object i do it through the vm

  • @ufukk54
    @ufukk54 3 ปีที่แล้ว +1

    Hi sir. How do I transfer data between pages with Dependency Injection? Can you make an app with 2 or 3 pages about it?

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +4

      That is a great idea Ufuk. I will try to do a mini series on it 🥳

  • @happyuser109
    @happyuser109 3 ปีที่แล้ว

    Really great video!

  • @mhmtkrnlk
    @mhmtkrnlk 2 ปีที่แล้ว

    MVVM architecture is realy feast to eyes. After UIKit

  • @aspublic
    @aspublic 2 ปีที่แล้ว

    Very useful video, but it would be great to have another video explaing dependency injection works with Core Data and a navigation of sub-views

  • @merlingrim2843
    @merlingrim2843 2 ปีที่แล้ว

    So even though you inject the dependency, the injected value/class instance is still effectively subject to threading issues.

    • @monstrositylabs
      @monstrositylabs 2 ปีที่แล้ว

      IMO yes. I find this pattern messy personally, but I seem to be the odd one out, so came here so that I'm down with the kids.
      The environmentObject would seem a cleaner way combined with @MainActor to pass data about, but I run up against more mess due to EnvironmentObject not easily accessible from the ViewModel.
      I'm yet to find the 'perfect' pattern of clean, safe code.

  • @vebbis5961
    @vebbis5961 2 ปีที่แล้ว +1

    How would you use dependency injection if you also had another service communicating with the service that communicates directly with the viewModel? Example:
    UserViewModel -> UserService -> StorageService/UserRepsoitory

    • @SwiftfulThinking
      @SwiftfulThinking  2 ปีที่แล้ว

      Great question! You would inject the UserRepository into the init of the UserService

    • @vebbis5961
      @vebbis5961 2 ปีที่แล้ว

      @@SwiftfulThinking Okay thank you. In that case would you be both using .sink in the ViewModel and the UserService? It just seems strange.

    • @SwiftfulThinking
      @SwiftfulThinking  2 ปีที่แล้ว +1

      @@vebbis5961 It all depends how you build your application. In this specific example, I’m not sure if a UserRespository needs to hold @Published variables. More common here, you would have for fetching functions in the UserRepository, which are being called from the UserService. But you probably don’t need to store them in memory (@Published) in both places. Often that’s only in the viewModel or the Service layer

    • @vebbis5961
      @vebbis5961 2 ปีที่แล้ว

      @@SwiftfulThinking Awesome. Thank you.

    • @vebbis5961
      @vebbis5961 2 ปีที่แล้ว

      @@SwiftfulThinking Have you considered creating a video on how to create a custom Publisher? There isn't a single tutorial on this with an in-depth explanation.

  • @egorsev
    @egorsev 3 ปีที่แล้ว +1

    Could you do video lessons about the Combine?

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +1

      I have some starter Combine videos in the "Continued Learning" playlist and I'll be adding some more advanced ones toward the end of this playlist!

    • @egorsev
      @egorsev 3 ปีที่แล้ว +1

      @@SwiftfulThinking Thank you so much for your hard work.

  • @ameliasaomarcos
    @ameliasaomarcos ปีที่แล้ว

    Nice.

  • @kevincosta2132
    @kevincosta2132 10 หลายเดือนก่อน

    All those videos using singletons just to know that we aren't actually going to use them in production D: great video tho!!

    • @SwiftfulThinking
      @SwiftfulThinking  10 หลายเดือนก่อน +1

      Most apps you work in will likely have a lot of Singletons. Although they are not preferred, they are very common. DI is preferred but it also requires much more code to support it, which is why I use Singletons in many videos, otherwise we’d spend too much time off-topic dealing with the DI 😅

  • @princeabyss4976
    @princeabyss4976 3 ปีที่แล้ว

    nice DI video, thankss 😁

  • @jarodpaulson9439
    @jarodpaulson9439 3 ปีที่แล้ว

    Great video man.

  • @dugrut1325
    @dugrut1325 ปีที่แล้ว

    I broke thru the 'DataService' barrier; ... how many times can you say 'DataService' before a programmer passes out? hahaha. I watched until the end but still no further ahead. So do I get the point? NO ; I'll watch it again, again an again

  • @stilljunior1764
    @stilljunior1764 ปีที่แล้ว

    i followed every line of code to 14:00, but my data isn't shown on the screen, but when I add a custom button to print it, everything works finally, actually, I don't understand why my screen doesn't update the data

    • @kflat420
      @kflat420 ปีที่แล้ว

      Can you post your code for the button. I'm having trouble getting the data to appear as well

  • @tbg6nb
    @tbg6nb 2 ปีที่แล้ว

    Shouldn't a singleton have a private init ? Otherwise you can create multiple instances

    • @SwiftfulThinking
      @SwiftfulThinking  2 ปีที่แล้ว

      This video is showing how to avoid using singletons, but yes you can have a private init if you are using one

    • @tbg6nb
      @tbg6nb 2 ปีที่แล้ว

      @@SwiftfulThinking Yeah, it is a very useful video and I really liked it but was just wondering if a class without a private init can be called a Singleton

  • @thahiraf1317
    @thahiraf1317 3 ปีที่แล้ว

    I am following your videos, xcode 13 making too much problem. what is the solution?

    • @SwiftfulThinking
      @SwiftfulThinking  3 ปีที่แล้ว +1

      My new videos will be in Xcode 13, but it shouldn’t make much of a difference..

    • @thahiraf1317
      @thahiraf1317 3 ปีที่แล้ว

      @@SwiftfulThinking sorry,may be I am feeling that because of fresher.

  • @OgabekDev
    @OgabekDev 7 หลายเดือนก่อน +1

    Your teaching skills are incredible. But this using protocols is not part of dependency injection. It is somehow polymorphism. But it is also great. I am your fan. I have another true subscriber.

  • @mhfs61
    @mhfs61 3 ปีที่แล้ว

    👍🏼👍🏼👍🏼

  • @dugrut1325
    @dugrut1325 ปีที่แล้ว

    I skipped this video, too deep! but I'll try it now... I'll let you know... anyone have a shovel?

  • @dugrut1325
    @dugrut1325 ปีที่แล้ว

    ...stressed you guys out... too late!

    • @SwiftfulThinking
      @SwiftfulThinking  ปีที่แล้ว +1

      What did u think?

    • @dugrut1325
      @dugrut1325 ปีที่แล้ว

      @@SwiftfulThinking after the first 5 minutes I passed out! But I won't give up. I've written a few big apps without this or understanding it. I don't understand init() ; combine; @EnvironmentObject and a few other big things but I work around it. Good thing I'm retired; I'd starve as a programmer. But I'll try to watch this video to the end. You are the best teacher out there, so I keep coming back to your videos.

    • @SwiftfulThinking
      @SwiftfulThinking  ปีที่แล้ว

      @@dugrut1325 😂😂😂😂 you’re cracking me up

    • @dugrut1325
      @dugrut1325 ปีที่แล้ว +1

      @@SwiftfulThinking Back in the early 80s through work I was introduced to BASIC which was a new concept. Which was right after recipe card hole punch computers. During that first class we learnt 5 command words only but I was hooked. I went home and through the night; I wrote a program. [on paper, no home computers then] I showed it to the instructor; he was impressed. After that I signed up at college to take a night course. There was only one main computer, which took up a wing of the school. The students had access to a keyboard entry terminal. It looked like a monster typewriter with paper. I wrote a program for work that created a guide chart. I showed it to the big bosses, they implemented across the country and it was used for twenty years. This started my IT career path. Until I became a permanent programmer and created major applications that were used in our federal agency through the whole country. VB VBA SQL and Digital CL. Most of this was self taught but I did classes in Montreal, Winnipeg, NY, etc ... I travelled the country all the time. It was a great job but I was happy to retire. Now I'm trying to learn SwiftUI. It is challenging. But I did write my LocationWeatherJournal which I use everyday. Also TravelTracker which works amazingly well, and a few others. Ok enough rambling...

  • @xpopcornx1747
    @xpopcornx1747 2 ปีที่แล้ว

    He skipped the most important part which is where and how to create the dependencies...

    • @dispatchswift
      @dispatchswift 2 ปีที่แล้ว

      He did explain where and how to create dependencies. Could you elaborate?