Modern Concurrency: Beyond the Basics

Oct 20 2022 · Swift 5.5, iOS 15, Xcode 13.4

Part 2: Concurrent Code

19. Challenge: Using a GlobalActor

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 18. Using a GlobalActor Next episode: 20. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

In this challenge, you’ll finish the debugging toolbar by connecting the second counter to display on-disk cache hits and making the last toolbar button clear the disk cache.

@MainActor private(set) var onDiskAccess: AsyncStream<Int>?
private var onDiskAccessCounter = 0 {
  didSet { onDiskAcccessContinuation?.yield(onDiskAccessCounter) }
}
private var onDiskAcccessContinuation: AsyncStream<Int>.Continuation?
func setUp() async throws {
  storage = await DiskStorage()
  for fileURL in try await storage.persistedFiles() {
    storedImagesIndex.insert(fileURL.lastPathComponent)
  }
  await imageLoader.setUp()
  🟩
  let accessStream = AsyncStream<Int> { continuation in
    onDiskAcccessContinuation = continuation
  }
  await MainActor.run { onDiskAccess = accessStream }
  🟥
}
deinit {
  onDiskAcccessContinuation?.finish()
}
onDiskAccessCounter += 1
onDiskAccessCounter = 0
print("Cleared disk cache.")
.task {
  guard let diskAccessSequence = ImageDatabase.shared.onDiskAccess else {
    return
  }
  for await count in diskAccessSequence {
    onDiskAccessCount = count
  }
}
Button(action: {
  // Clear on-disk cache
  🟩
  Task {
    await ImageDatabase.shared.clear()
  }
  🟥
}, label: {
  Image(systemName: "folder.badge.minus")
})