How to use Global Actors in Swift (@globalActor) | Swift Concurrency #10

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ม.ค. 2025

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

  • @w0mblemania
    @w0mblemania 10 หลายเดือนก่อน +4

    @16:19 According to the Swift Proposal, "A global actor type can be a struct, enum, actor, or final class. It is essentially just a marker type that provides access to the actual shared actor instance via shared".

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

    This has been a great series of videos as I have gotten used to enjoying from you. Having built several large apps there are certain things that are great candidates for global actors. In fact, most of the current singletons I use are to access a server somewhere and share the data throughout the app. Thanks for boiling this down to the essentials and presenting it so well.

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

    Dude, thanks a lot for making these videos. The quality and curation of content are spot on.

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

    Every time I come to these videos I learn and understand more. Thank you so much Nick for these highly reliable videos.

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

    Awesome content Nick! Regarding your question of whether to use a final class or structure for your singleton [16:50]? According to Stackoverflow answer 36788169 , classes are preferred to structures when working with singletons. One of the posts illustrates this with an example.

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

    Paused at 17:50. For one time use actor MyNewDataManager I recommend putting it and MyFirstGlobalActor in the file and marking MyNewDataManager as fileprivate. That should keep it from being accidentally used.

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

    thank you so much for this whole playlist i was having a hard time understanding stuff but these are so well explained that i understand it now

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

    Another excellent video. Can't wait to watch the last 2 videos

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

    This video explains well how the MainActor functionality is just built on top of the globalActor API, but with this simplistic example I've still no clue in which scenario I should create my own globalActor. Does anyone know a video or article with an more realistic example that clearly shows what problem this can solve?

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

    Great series, very well explained. A question is have @10:50, globalactor must be singleton. @12:07, we are using a struct as globalactor and makes it singleton. I wonder why we use a struct (value type and not a reference type) as a singleton? Doesn't singleton negate the purpose of value type that can copy its value and make a new instance?

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

    Thanks, Nick, for creating outstanding content.

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

    Thanks, this video is perfect to better understand the use of MainActor

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

    Since globalActors are by definition a single instance this leads me to believe they should be structs for access time reasons. Profile several use case or your particular use case may be the best answer here because there is a certain amount of optimizations going on internally. There was a WWDC video about this.

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

      Can you give me a link to this point at WWDC ?

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

      @@hashcat5721 I’ll what can find. My next few days are busy and since WWDC isnt fresh in my mind now its going to taking a bit of digging. In the mean time you may wantbto google WWDC GlobalActors, there cant be much out there on this.

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

    16:50 What about using there`@globalActor actor MyNewDataManager { }` where init is private and all logic is in this actor? Isn't that a better solution?

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

    Thank you for these amazing series. Really helps me as a Junior iOS developer

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

      Do jr need this for entry level ?

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

      @@natgenesis5038 none of the interviews asked me about global actors. But not bad to know I guess

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

    I love your videos so much and there very useful. There is so little good tutorials on TH-cam, is there anyway you could make a video or two on implementing the camera in SwiftUI as a full screen.

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

    A global actor can be a struct, enum, actor, or final class. (Source: SE-0316)

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

    awsome as always

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

    Is there a disadvantage of putting mainactor on published?

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

    This made a lot of sense, right until the point where I switched to Swift 6, now I’m struggling to understand all the weird errors that pops up.
    Understanding and working with GCD and all its weird quirks is much less complicated than swift concurrency at this point.

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

    It needs to get way more Thumbs up.
    Friends, 11K views and only 461 likes. More we give likes, more users will see such videos.

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

    7:54 this is incredibly confusing, why is it running on the main thread, it is async? And are you treating main actor = main thread?

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

      It is not running on the main thread. It's a mistake from the author. The viewModel.getData() method is awaited on a background thread. If the function, or the whole class was explicitly marked with @MainActor, then yeah, that would be running on the main thread and also blocking the UI.

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

    Now I know how to use nonIsolated and Global Actors but I still don’t understand their use cases. 😢

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

    Single Instance? Whats If I want to have separate instances per Account in a multi account app? Horrible api

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

      Can you explain further? What specifically is a horrible api? What would you propose as an alternative?

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

      In that case you would simply design a different class.
      There's a reason to use singletons (and reasons not to).
      It's YOUR job as a programmer to know how to apply the tools.

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

    First

    • @veens.m
      @veens.m 2 ปีที่แล้ว

      Good Boy 👏🏼

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

    I'm becoming an intermediate Swift developer having followed your courses since January and written some fairly heavy apps. Now I'm starting to get my code Swift 6 ready, and learning more about actors, I'm wondering if, in the MVVM model, should all ViewModel classes be tagged at @MainActor?

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

    Is there any reason why you can not/should not add @globalActor on MyNewDataManager and make it a singleton to avoid risking the issue you called out with multiple instances of it?

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

    is @MainActor private lazy var mapView: MKMapView = {
    let mapView = MKMapView()
    mapView.translatesAutoresizingMaskIntoConstraints = false
    mapView.delegate = self
    mapView.showsCompass = false
    mapView.showsUserLocation = true
    return mapView
    }()
    this a valid usage? because it don't give any errors but also if I mark this with await, xcode warns me that no async process occurs

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

    Why nick doesn’t like to use singleton he didn’t give a reason why. Can somebody tell me ?

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

      Explained here: th-cam.com/video/E3x07blYvdE/w-d-xo.html

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

    "The @MainActor attribute should be placed before the @Published property wrapper. " -ChatGPT-