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
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 18. Using a GlobalActor Next episode: 20. Conclusion

This video Challenge: Using a GlobalActor was last updated on Oct 20 2022

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

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

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")
})