Code: Select all
Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure
Code: Select all
MovieRecorder
Code: Select all
/// Starts movie recording.
func startRecording() {
Task {
// Return early if already recording.
guard await !movieRecorder.isRecording else { return }
await movieRecorder.startRecording()
// Start a timer to update the recording time.
startMonitoringDuration()
}
}
// Starts a timer to update the recording time.
private func startMonitoringDuration() {
captureActivity = .movieCapture()
timerCancellable = Timer.publish(every: refreshInterval, on: .main, in: .common)
.autoconnect()
.sink { [weak self] _ in
guard let self else { return }
// Poll the movie output for its recorded duration.
Task {
let duration = await self.movieRecorder.recordedDuration.seconds
self.captureActivity = .movieCapture(duration: duration)
}
}
}