Why You Should Use The SwiftUI Coordinator Pattern

Sdílet
Vložit
  • čas přidán 1. 06. 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!
    → czcams.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
  • Věda a technologie

Komentáře • 63

  • @rebeloper
    @rebeloper  Před rokem +1

    Firebase Firestore in SwiftUI Demystified 👉 czcams.com/users/livezSuEvcXbBTw?feature=share

  • @YourLaw
    @YourLaw Před 11 měsíci +1

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

    • @rebeloper
      @rebeloper  Před 11 měsíci

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

  • @davidcbeaudoin
    @davidcbeaudoin Před rokem +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  Před rokem +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=CZcams

  • @stevenkirke
    @stevenkirke Před 2 měsíci

    Much appreciated for the very explanatory tutorial!

    • @rebeloper
      @rebeloper  Před 2 měsíci

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

  • @bas3485
    @bas3485 Před rokem

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

    • @rebeloper
      @rebeloper  Před rokem

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

  • @cagrgider8320
    @cagrgider8320 Před rokem +1

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

    • @rebeloper
      @rebeloper  Před rokem

      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=CZcams
      Expires January the 31st

  • @AppDeveloperPoint
    @AppDeveloperPoint Před rokem

    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  Před rokem

      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 👇
      czcams.com/video/kF8Qk28-FFU/video.html

    • @AppDeveloperPoint
      @AppDeveloperPoint Před rokem

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

    • @rebeloper
      @rebeloper  Před rokem

      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! 👇
      czcams.com/video/4149Ru4H4bI/video.html

    • @star_maze
      @star_maze Před rokem

      @@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 Před 2 měsíci

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

    • @rebeloper
      @rebeloper  Před měsícem

      Thanks for the tip. What do you think about this one: czcams.com/video/sikBA9n5H2k/video.html

  • @user-uk1db9tm2g
    @user-uk1db9tm2g Před 8 měsíci

    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  Před 8 měsíci

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

  • @HINDISE_ENGLISHTAK
    @HINDISE_ENGLISHTAK Před rokem

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

    • @rebeloper
      @rebeloper  Před rokem

      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! 👇
      czcams.com/video/3nHK5ctLCNE/video.html

  • @AdelUI
    @AdelUI Před 9 měsíci

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

    • @rebeloper
      @rebeloper  Před 9 měsíci

      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 👇
      czcams.com/video/9lRWau51lGw/video.html&ab_channel=Rebeloper-RebelDeveloper

  • @DragunXtreme
    @DragunXtreme Před 10 měsíci +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  Před 10 měsíci +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 👇
      czcams.com/video/nrtr14iOj0E/video.html&ab_channel=Rebeloper-RebelDeveloper

  • @russellwarwick
    @russellwarwick Před 5 měsíci

    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  Před 4 měsíci +1

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

    • @russellwarwick
      @russellwarwick Před 4 měsíci +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  Před 4 měsíci

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

  • @BabuLal-bf6oq
    @BabuLal-bf6oq Před rokem +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  Před rokem

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

  • @sami790311
    @sami790311 Před 9 měsíci +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  Před 9 měsíci

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

    • @alexthurston9940
      @alexthurston9940 Před 9 měsíci +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.

    • @Hgvpuncher
      @Hgvpuncher Před 2 měsíci

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

  • @TheClampet23
    @TheClampet23 Před rokem

    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  Před rokem

      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! 👇
      czcams.com/video/LUsybFHC4Co/video.html&ab_channel=Rebeloper-RebelDeveloper

  • @NeerajGurjar-dx1rr
    @NeerajGurjar-dx1rr Před 9 měsíci

    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  Před 9 měsíci

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

  • @sergioudalov361
    @sergioudalov361 Před rokem

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

    • @rebeloper
      @rebeloper  Před rokem

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

  • @hosseindastan6221
    @hosseindastan6221 Před rokem

    what about passing ID for specific view?

    • @rebeloper
      @rebeloper  Před rokem

      Why would you do that?
      Let me show you a Progress Button in SwiftUI 👇
      czcams.com/video/kF8Qk28-FFU/video.html

  • @book6202
    @book6202 Před rokem

    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  Před rokem

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

  • @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  Před rokem

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

    • @TharinduRameshKetipearachchi
      @TharinduRameshKetipearachchi Před rokem

      @@rebeloper Thanks ☺

    • @TharinduRameshKetipearachchi
      @TharinduRameshKetipearachchi Před rokem

      @@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  Před rokem

      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! 👇
      czcams.com/video/msjk6CJpcY8/video.html&ab_channel=Rebeloper-RebelDeveloper

  • @WuatDaFack
    @WuatDaFack Před 7 měsíci

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

    • @rebeloper
      @rebeloper  Před 7 měsíci

      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 👇
      czcams.com/video/supzAhPiDZ8/video.html&ab_channel=Rebeloper-RebelDeveloper

  • @iansmith9368
    @iansmith9368 Před 9 měsíci

    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  Před 9 měsíci

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

  • @nicolaiharbo2201
    @nicolaiharbo2201 Před 7 měsíci

    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  Před 7 měsíci

      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 👇
      czcams.com/video/9PXR5iXX-7k/video.html&ab_channel=Rebeloper-RebelDeveloper

  • @redhotbits
    @redhotbits Před 9 měsíci

    why?

    • @rebeloper
      @rebeloper  Před 9 měsíci

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

  • @StianF
    @StianF Před 2 měsíci +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  Před 2 měsíci

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

  • @AryamanSharda
    @AryamanSharda Před měsícem

    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  Před měsícem

      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! 👇
      czcams.com/video/glg1koMSyLA/video.html&ab_channel=Rebeloper-RebelDeveloper