Why You Should Use The SwiftUI Coordinator Pattern

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ส.ค. 2024
  • Swift Coordinator Pattern
    Escape Tutorial Hell 👉 rebeloper.com/mentoring
    SwiftUI Camp 👉 store.rebeloper.com/swiftui-c...
    In this video, we'll be introducing the Swift Coordinator Pattern. This pattern is used to manage the life cycle of navigation in a program. We'll walk you through the different aspects of the pattern and how to use it in your code.
    If you're interested in learning more about the Swift language and how to write better code, then this video is for you! By the end of this video, you'll have a better understanding of the Swift Coordinator Pattern and how to use it in your SwiftUI projects.
    Don’t forget to download the resources:
    → store.rebeloper.com/youtube-c...
    CHAPTERS:
    _____
    00:00 INTRO
    00:34 SWIFTUI CAMP
    01:00 FINAL PROJECT DEMO
    02:11 STARTER PROJECT
    02:40 COORDINATOR
    03:18 COORDINATOR PAGE
    04:12 COORDINATOR SHEET
    04:30 COORDINATOR FULL SCREEN COVER
    04:50 COORDINATOR CLASS
    05:55 PUSH WITH THE COORDINATOR PATTERN
    06:23 PRESENT A SHEET WITH THE COORDINATOR
    06:46 PRESENT A FULL SCREEN COVER IN SWIFTUI
    07:12 POP WITH THE COORDINATOR PATTERN
    07:30 POP TO ROOT IN SWIFTUI
    07:52 DISMISS A SHEET WITH THE COORDINATOR
    08:05 DISMISS A FULL SCREEN COVER IN SWIFTUI
    08:40 BUILD VIEWS FOR THE COORDINATOR PATTERN
    10:15 COORDINATOR VIEW
    11:00 NAVIGATION STACK WITH THE COORDINATOR
    12:00 SHEETS IN THE COORDINATOR PATTERN
    12:33 FULL SCREEN COVER WITH COORDINATOR
    13:20 PUSH A VIEW WITH THE COORDINATOR PATTERN
    14:53 PRESENT A SHEET IN SWIFTUI AND COORDINATOR
    15:12 PRESENT A FULL SCREEN COVER WITH COORDINATOR
    15:25 DISMISS A MODAL WITH THE COORDINATOR PATTERN
    15:58 SHOWING THE NAVIGATION TITLE IN SWIFTUI
    16:54 PUSH AND POP WITH THE COORDINATOR
    17:48 POP TO ROOT IN SWIFTUI AND COORDINATOR PATTERN
    18:17 WHERE TO GO NEXT
    **VIDEOS I RECOMMEND**
    → PLAYLIST:
    Senior iOS Developer Secret:
    • 5 Levels of SwiftUI Sk...
    DO YOU WANT TO BE MY APPRENTICE?
    HIRE ME → rebeloper.com/hire-us/
    TOOLS I RECOMMEND:
    → rebeloper.com/tools
    __________
    Get in touch:
    → support@rebeloper.com
    SUBSCRIBE to weekly tips & tutorials for building iOS apps!
    → th-cam.com/users/rebeloper?su...
    GitHub: github.com/rebeloper/
    Hire me: rebeloper.com/hire-us/
    LinkedIn: / rebeloper
    My Blog: rebeloper.com/blog
    Follow me on Instagram: / rebeloper
    ______
    #rebeloper
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    Firebase Firestore in SwiftUI Demystified 👉 th-cam.com/users/livezSuEvcXbBTw?feature=share

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

    I have watched a lot of your videos, this is your best video. Clean crisp explanation of a topic.

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

      Glad you liked it.
      Next learn about @Observable @Bindable in iOS17 - SwiftUI Data Flow 👇
      th-cam.com/video/Igblg7c2T1o/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    I'm very much a SwiftUI beginner, so I'm still working on wrapping my head around much of the basics. This is a great, easy to understand approach to navigation. Thank you for the great tutorial!

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

      Glad it was helpful!
      If you want to boost your learning check out SwiftUI Camp: store.rebeloper.com/swiftui-camp-habit-tracker-edition?coupon=TH-cam

  • @Xcoder-Ayush
    @Xcoder-Ayush 6 วันที่ผ่านมา

    Great tutorial! I have enhanced this to pass data from one View to another in this coordinator pattern in SwiftUI. 😀

    • @rebeloper
      @rebeloper  วันที่ผ่านมา

      That's great!
      Have you seen my latest video? 👉 NavigationStack vs. NavigationCoordinator: You Won’t Believe the Difference!
      th-cam.com/video/7fb7CXty5i4/w-d-xo.html

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

    Much appreciated for the very explanatory tutorial!

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

      You're very welcome!
      Next watch: SwiftUI Text - EVERYTHING You Need to Know 👇
      th-cam.com/video/rKEfs5j3ANE/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

    • @glnygl8525
      @glnygl8525 12 วันที่ผ่านมา

      Hi Steven, I faced same problem as yours. Fetching data several times when I go to detail and come back. I tried to fix this using a propery like did load which is worked but Im still curious about how did you solve this and looking for a better way

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

    Nice video! I like the idea. Do you also have videos about navigation for lower deployment targets? I could use some inspiration

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

      Yes I do! Check out my store: store.rebeloper.com
      Let me show you a Progress Button in SwiftUI 👇
      th-cam.com/video/kF8Qk28-FFU/w-d-xo.html

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

    That looks clean, thanks a lot. But how can we pass a value to the following view with this pattern?

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

      Good question! For that you might want to check out NavigationRouter: store.rebeloper.com/navigation-router
      Or SwiftUI Camp (50% OFF discount in the link): store.rebeloper.com/swiftui-camp-twitter-edition?coupon=TH-cam
      Expires January the 31st

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

    How I can set parameters in a View using this pattern? I like very much how you control the navigation is simple and direct, but the views that you use in this example don't receive any argument and I don't no how adapter your Enum Page to receive some parameters depends of View called. But congratulations for the video, your explanation is wonderful

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

      You can add a dependency on the enum value. I can show you how on a mentoring session here: rebeloper.com/mentoring
      Next check out SwiftUI Navigation Perfected: Harnessing NStack's Game-changing Capabilities 👇
      th-cam.com/video/nrtr14iOj0E/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    Thank you very much for the video. How would you handle a situation with child coordinators though? Would you create CoordinatorViews for each and instantiate the coordinators there too?

    • @rebeloper
      @rebeloper  วันที่ผ่านมา

      I avid child coordinators like the plague :)
      Have you seen my latest video? 👉 NavigationStack vs. NavigationCoordinator: You Won’t Believe the Difference!
      th-cam.com/video/7fb7CXty5i4/w-d-xo.html

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

    Nice Explanation! how about popTo any particular view like popToThirdView, popToSecondView etc ?

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

      You can do all of those with NavigationRouter found in my community: store.rebeloper.com/community
      If you're a SwiftUI developer you will love: Unveiling The 10 Commandments of SwiftUI - #4 Will Surprise You! 👇
      th-cam.com/video/3nHK5ctLCNE/w-d-xo.html

  • @BabuLal-bf6oq
    @BabuLal-bf6oq ปีที่แล้ว +1

    It's really nice explanation but can you please let me know how to pass data to another view using this coordinator pattern.

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

      Check out NavigationRouter from kindaSwiftUI:
      github.com/rebeloperAlpar/kindaSwiftUI/wiki/NavigationRouter
      Next Unlock the Power of Core Data Versioning! 👇
      th-cam.com/video/RDtLWWos3cA/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @glnygl8525
    @glnygl8525 12 วันที่ผ่านมา

    Thank you for video. But here is an issue if you want to fetch data in .task or onAppear views are recreating several times so app is trying to fetch data again again again. We can handle this problem with a state property like isLoaded or something but do we need this really or do you have a better idea to solve this problem?

    • @rebeloper
      @rebeloper  วันที่ผ่านมา

      That is a great solution. I actually have a view modifier for my client projects to solve this issue.
      Have you seen my latest video? 👉 NavigationStack vs. NavigationCoordinator: You Won’t Believe the Difference!
      th-cam.com/video/7fb7CXty5i4/w-d-xo.html

  • @user-uk1db9tm2g
    @user-uk1db9tm2g 10 หลายเดือนก่อน

    Any chance you've found a fix the white flash this navigation system now produces in iOS17? Prior releases of iOS worked perfectly, but now there's an annoying white flash on any pop(). Been messing around with several alternatives to update the background color, but have been unsuccessful so far.

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

      Must wait for an iOS update for this fix :(
      Next watch -> From Boring to Breathtaking: Mastering SwiftUI Splash Screens for Remarkable App Launches 👇
      th-cam.com/video/lBCpwYDljwI/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    Thanks for this video. I have one question here, How you will manage the switch case fro large complex app when you need to route to say 50 screens. ?

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

      This can be used for 5 or 50 screens too. Number of screens does not matter.
      Let me show you a Progress Button in SwiftUI 👇
      th-cam.com/video/kF8Qk28-FFU/w-d-xo.html

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

      @@rebeloper yes we can write 100 cases. But will that be a good practice?

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

      I don't see why it could be a bad practice.
      BTW check out my lates video 👉 2023's Must-Have Xcode Extensions: Revealed Here! 👇
      th-cam.com/video/4149Ru4H4bI/w-d-xo.html

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

      @@AppDeveloperPoint in that case you need to consider the modular approach to your architecture where each module will have its coordinator. The global coordinator will handle routes between the modules.

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

    Nice video. But some tips, in Coordinator View & ViewModel should also be coupled tightly.

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

      Thanks for the tip. What do you think about this one: th-cam.com/video/sikBA9n5H2k/w-d-xo.html

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

    First of all, thanx for amazing contents , however, what's the different between coordinator pattern & Routes ?

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

      In my view no difference between them. It's just Coordinator sound more techy :P
      BTW have you seen Firebase Login in SwiftUI: Step by Step Masterclass 👇
      th-cam.com/video/9lRWau51lGw/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    I like the example. However, there is one thing that still creates a strong coupling between views. Despite the fact that the coordinator is taking care of the navigation, the views are still knowing about other views. What if from BananaView I want to present different views depending on the case? The views should be agnostic about other views. This way they can be reused.
    I think adding to the coordinator pattern the composition pattern could solve the problem.

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

      Thank you for the feedback. What do you think of the next video: Dependency Injection in SwiftUI, the Best Pattern: th-cam.com/video/Ldrf81y41pw/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

      This is a problem with SwiftUI since we need to type the view hierarchy for performance reasons. I’m not sure of any way around it beside AnyView which is bad for performance. Maybe someday! Otherwise you can use UIKit for navigation under the hood.

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

      @@alexthurston9940 Why do you think AnyView is bad for perfomance? AnyView is fine for a small project

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

    Will this code work for the cases when I need to open a sheet inside a sheet, a fullscreen cover inside a sheet, a sheet inside a fullscreen cover, and so on?

    • @rebeloper
      @rebeloper  วันที่ผ่านมา

      Not at this stage of the code, but Navigation Coordinator does:
      th-cam.com/video/7fb7CXty5i4/w-d-xo.html

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

    trying to present a sheet from another sheet with this method, but the app dismisses the first sheet before presenting the new one. how would you solve for that?

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

      A sheets should be presented upon the change of a Published variable. Use different ones for more sheets. If you need more help consider booking a mentoring session: rebeloper.com/mentoring
      Next Learn About One Fatal Programming Mistake: Stop Making it a Hobby! 👇
      th-cam.com/video/LUsybFHC4Co/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @NeerajGurjar-dx1rr
    @NeerajGurjar-dx1rr ปีที่แล้ว

    It is very helpful but what if we need to send data from one view to another view how can we do this. can you help

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

      Here you go: Dependency Injection in SwiftUI, the Best Pattern: th-cam.com/video/Ldrf81y41pw/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    Nice example! However the view still knows about its navigation path. Coordinator should also address tight coupling of view and navigation. Any thoughts on this?

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

      I have a solution for that (it's paid): th-cam.com/video/nrtr14iOj0E/w-d-xo.htmlsi=xUPBOBdeVox-C7FO
      Next Discover the 5 Best View Extensions in SwiftUI 👇
      th-cam.com/video/n9Amll3lY9I/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

    • @russellwarwick
      @russellwarwick 7 หลายเดือนก่อน +2

      Nice concept with NStack however it doesn’t really solve the problem of the View knowing about its navigation path. The View should be dumb so it can be reused elsewhere without attaching all of that navigation logic. The view should only know that a button was pressed or an action was performed. It’s then the responsibility of a Coordinator to dictate the next step, not the view. I’m not sure if it’s a limitation with SwiftUI though.

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

      Good points. I might add this into NStack at some point in the future.
      Next find out the Top 24 Xcode Shortcuts For Beginners 👇
      th-cam.com/video/6TuKotMdxHQ/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    what about passing ID for specific view?

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

      Why would you do that?
      Let me show you a Progress Button in SwiftUI 👇
      th-cam.com/video/kF8Qk28-FFU/w-d-xo.html

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

    Apple should provide such classes in early version of iOS atleast 14.0 but it's available on iOS 16.0.
    Most of product base iOS versions supports 13.0.

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

      Totally agree.
      Did you wonder if will AI replace programmers? Check out my take on it 👇
      th-cam.com/video/9NlECBGI-OM/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    looks nice, but what if we got 100 screens?) it will be very huge enum...)

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

      Still clearer than having those 100 screens scattered all of=ver the project, right?
      Let me show you a Progress Button in SwiftUI 👇
      th-cam.com/video/kF8Qk28-FFU/w-d-xo.html

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

    every time I push a page and then I use the back button my app crashes, anybody knows why?

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

      I would need to take a look at your full code. Book a session here: rebeloper.com/mentoring
      🚨 A Subscriber Sent Me a Junior Swift Interview Challenge, Let's Solve It 👇
      th-cam.com/video/supzAhPiDZ8/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    I think this isn't thread safe
    @Published var path = NavigationPath()
    Xcode giving me the following error
    Publishing changes from within view updates is not allowed, this will cause undefined behavior.

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

      Put @MainActor on your class :)
      Here's Why You Shouldn't Nest Your Code 👇
      th-cam.com/video/EceQfX2e0mU/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

      @@rebeloper Thanks ☺

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

      @@rebeloper Still I'm getting that warning
      Publishing changes from within view updates is not allowed, this will cause undefined behavior.
      This is coming for following line
      @Published var path = NavigationPath()

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

      I can take a look at your code on a mentoring session and debug it: rebeloper.com/mentoring
      Next check out WWDC23: You're NOT Ready for this WWDC23 Wish List! 👇
      th-cam.com/video/msjk6CJpcY8/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    This wouldn't work, if you want to present a sheet from a detail view. Then the coordinator will present that sheet from the initial view, leading to a warning in the console

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

      Thanks for letting me know. Since than I have perfected a library for SwiftUI Navigation: store.rebeloper.com/nstack
      BTW watch me take my own SwiftUI test here 👇
      th-cam.com/video/9PXR5iXX-7k/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    I have tried this pattern using NavigationLinks instead of buttons, but the environment doesn't get passed 'down'. Perhaps NavigationLink doesn't create a child.

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

      Maybe you're not using the latest SwiftUI. Check out a solution for all SwiftUI versions here: th-cam.com/video/nrtr14iOj0E/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper
      BTW have you seen Firebase Login in SwiftUI: Step by Step Masterclass 👇
      th-cam.com/video/9lRWau51lGw/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    why?

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

      For me it makes my code cleaner.
      Stop using Spacer in SwiftUI (and what to use instead) 👇
      th-cam.com/video/S6bufNjIteQ/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

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

    If this screen is reused elsewhere in the app, in a different user flow for example, then specifying the Coordinator in the View makes it so coupled that I wouldn't be able to reuse this view anymore right? I wouldn't be able to use it with both CoordinatorA and CoordinatorB.
    The equivalent `Page` enum for CoordinatorA and CoordinatorB may have entirely different cases so specifying a single Coordinator and making calls like `coordinator.push(.banana)` make it so I can't reuse this view in any other flow in my app.

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

      Check out this new version and let me know what you think: store.rebeloper.com/navigation-coordinator
      Next check out my latest video about Mastering SwiftUI Error Alerts: Fix Your App in Seconds! 👇
      th-cam.com/video/glg1koMSyLA/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @StianF
    @StianF 5 หลายเดือนก่อน +1

    What about actual use cases where data needs to be passed back and forth, or where a single view can be presented as both page and sheet and cover? We gotta pay $2000 dollars to learn? Bullshit moneyscam

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

      Use an EnvironmentObject to access data from any of the views.
      Next escape the GeometryReader nightmare 👇
      th-cam.com/video/zf-Qnx4laT4/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper