Dependency Injection in SwiftUI, the Best Pattern

  Рет қаралды 4,541

Rebeloper - Rebel Developer

Rebeloper - Rebel Developer

Күн бұрын

Пікірлер: 19
@rebeloper
@rebeloper Жыл бұрын
SwiftUI Navigation Perfected: Harnessing NStack's Game-changing Capabilities 👉 kzbin.info/www/bejne/pKPXo2RqnrSdZqc&ab_channel=Rebeloper-RebelDeveloper
@rifatkhadafy5646
@rifatkhadafy5646 4 ай бұрын
finally this is should be primary example of using navigation stack
@rebeloper
@rebeloper 4 ай бұрын
Have you seen my latest video? 👉 NavigationStack vs. NavigationCoordinator: You Won’t Believe the Difference! kzbin.info/www/bejne/bZfFaHaOqd5on5Y
@mariomastrandrea4165
@mariomastrandrea4165 3 ай бұрын
Hi! I really appreciated your example of pattern using the Coordinator, but I have some doubts regarding its use when various dependencies come into play: what happens when the views have some 'external' dependencies? In real world scenarios, it is likely that a page has multiple dependencies, some of them being some Services instances managing side effects and interacting with the external world (e.g. API calls), and not just pure data models like in your example (the Profile struct). How would you manage (the injection of) these dependencies in this case? For example, if the DetailView had 2 services (e.g. ItemsService and LocationService), which are specific to the DetailView and not directly used by the parent ContentView, you cannot create them inside the ContentView itself this time and inject them into a Page instance by the coordinator of course. - So would you inject them from the coordinator build() method? If yes, how? Would you store a reference of these services directly inside the Coordinator instance? - Or is it maybe better to avoid the 'build()' method, and just instantiate the views inside the .navigationDestination(), injecting there the 2 services (coming from the CoordinatorView)? Thank you very much in advance. I would love to use this pattern, but I have these concerns when coming to real complex projects
@rebeloper
@rebeloper 2 ай бұрын
I would use a Controller: kzbin.info/www/bejne/aHWTZpd9pJideJYsi=kTqX3rZV_5EiUxOm together with NavigationCoordinator: kzbin.info/www/bejne/gYW8kGWcaJiFY6Msi=t_tDV3IXs5Vq-upx
@ZachMatics
@ZachMatics Жыл бұрын
I've been trying to implement this in Xcode 15 beta with Observable macro on Coordinator class but it doesn't seem to be working. I reproduced your more robust Coordinator setup (different video) and am now trying to pass a Profile object (from CoordinatorView to Views in NavigationStack) that is stored with SwiftData. In essence, I want to implement this Coordinator pattern and leverage dependency injection with the latest tools from Apple. Not sure if the complications that I'm running into are surmountable.
@ZachMatics
@ZachMatics Жыл бұрын
Update: I've been subtracting complexity from my implementation while trying to preserve the Observable macro and ESB architecture (removed the passing of SwiftData for test Views). Still not working for me, however. The first View is shown as expected but subsequent navigations are unsuccessful.
@ZachMatics
@ZachMatics Жыл бұрын
Ok, I've replicated your implementation successfully with non-ESB pattern. My problem, then, boils down to trying to implement Coordinator pattern with ESB architecture.
@rebeloper
@rebeloper Жыл бұрын
For the Coordinator you doo need some sort of Environment. The Coordinator can live side by side the ESB pattern BTW have you seen Firebase Login in SwiftUI: Step by Step Masterclass 👇 kzbin.info/www/bejne/b521iJSrapaffdk&ab_channel=Rebeloper-RebelDeveloper
@guillem.apellaniz458
@guillem.apellaniz458 Жыл бұрын
Thanks!
@rebeloper
@rebeloper Жыл бұрын
Welcome! BTW have you seen Firebase Login in SwiftUI: Step by Step Masterclass 👇 kzbin.info/www/bejne/b521iJSrapaffdk&ab_channel=Rebeloper-RebelDeveloper
@kiohaha
@kiohaha 4 ай бұрын
Matrix moment here 13:24 😂
@rebeloper
@rebeloper 4 ай бұрын
😁 Have you seen my latest video? 👉 NavigationStack vs. NavigationCoordinator: You Won’t Believe the Difference! kzbin.info/www/bejne/bZfFaHaOqd5on5Y
Firebase Login in SwiftUI: Step by Step Masterclass
53:10
Rebeloper - Rebel Developer
Рет қаралды 1 М.
How to use Dependency Injection in SwiftUI | Advanced Learning #16
35:32
Swiftful Thinking
Рет қаралды 30 М.
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 23 МЛН
Why You Should Use The SwiftUI Coordinator Pattern
18:45
Rebeloper - Rebel Developer
Рет қаралды 16 М.
MVVM + Dependency Injection in Swift | Unit Testing | iOS
29:29
Swinject: Effortless Dependency Injection in iOS (Basics)
12:45
iOS Academy
Рет қаралды 29 М.
Field Validation and MVVM in SwiftUI
19:04
Stewart Lynch
Рет қаралды 15 М.
Swift Dependency Injection - What is it? What are the benefits?
6:58
SwiftData Basics in 15 minutes
15:18
CodeWithChris
Рет қаралды 51 М.
Learn Dependency Injection for SwiftUI / UIKit (iOS Tutorial)
21:05