# Chapter 9 Global Actors and ImageLoader contrived or?

**URL:** <https://forums.kodeco.com/t/chapter-9-global-actors-and-imageloader-contrived-or/190177>\
**Category:** Modern Concurrency in Swift\
**Created:** [August 6, 2023, 11:37pm UTC](https://forums.kodeco.com/t/chapter-9-global-actors-and-imageloader-contrived-or/190177 "2023-08-06T23:37:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![grangej](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/grangej/32/108944_2.png) [@grangej](https://forums.kodeco.com/u/grangej)\
**Post date:** [August 6, 2023, 11:37pm UTC](https://forums.kodeco.com/t/chapter-9-global-actors-and-imageloader-contrived-or/190177/1 "2023-08-06T23:37:43Z")

</div>

After going through the material in Chapter 9 I was curious if the example of using the AsyncStream in the global actor was the correct approach ? Doing this required extra “setup” and some boilerplate code it seems, wouldn’t it be more concise to use @Published here since the we are tying the updated value to the UI anyway? I’m trying to understand if there is some advantage of one or the other.

```auto
// ImageLoader
  @MainActor @Published var testCount: Int = 0
  
  func incrementTestCount() async {
    await MainActor.run { testCount += 1 }
  }

// BotomToolBar

  @ObservedObject var testVar = ImageDatabase.shared.imageLoader
....
  Text("Access: \(onDiskAccessCount) from disk, \(testVar.testCount) in memory")

```

vs

```auto
\\ ImageLoader
  @MainActor private(set) var inMemoryAccess: AsyncStream<Int>?
  
  private var inMemoryAccessContinuation: AsyncStream<Int>.Continuation?
  private var inMemoryAccessCounter = 0 {
    didSet { inMemoryAccessContinuation?.yield(inMemoryAccessCounter) }
  }

  deinit {
    inMemoryAccessContinuation?.finish()
  }

  func setup() async {
    let accessStream = AsyncStream<Int> { continuation in
      inMemoryAccessContinuation = continuation
    }
    await MainActor.run { inMemoryAccess = accessStream }
  }

// BotomToolBar

  @State var inMemoryAccessCount = 0

      Text("Access: \(onDiskAccessCount) from disk, \(inMemoryAccessCount) in memory")

    .task {
      guard let memoryAccessSequence =
        ImageDatabase.shared.imageLoader.inMemoryAccess else {
        return
      }
      for await count in memoryAccessSequence {
        inMemoryAccessCount = count
      }
    }

```
