@libei @jjoelson @mattiem I don't think it would be. Task.init's closure is marked @_inheritActorContext (https://github.com/apple/swift/blob/main/stdlib/public/Concurrency/Task.swift#L666), so after sleep returns, the actor is running the synchronous code in the task and anyone else calling update(offset:) would suspend until that's done. and Task.sleep checks for cancellation immediately after it unsuspends, so even if another update(offset:) call occurs while the current task is sleeping—and that new call executes first, before the synchronous code in the closure—the current task will end early when it unsuspends
swift/stdlib/public/Concurrency/Task.swift at main · apple/swift

The Swift Programming Language. Contribute to apple/swift development by creating an account on GitHub.

GitHub
@shadowfacts @libei @mattiem Now I’m having doubts actually, because in order to call `update(offset:)` you need to do something like this in the UI, and as far as I know there’s no guarantee as to the ordering of these MainActor tasks if they’re being generated very quickly by scroll events:
@jjoelson @libei @mattiem hmm, yeah I think you're right. I think I'm with Matt that the actor is confusing things here.

tbh, what I'd do (have done) is go back to storing the Task in the view controller and instead of capturing the offset, read it from the scroll view. it might, strictly speaking, race, but the only effect would be writing the current position more than necessary. so:

task?.cancel()
task = Task.detatched { [unowned self] in
try await Task.sleep(for: .seconds(1))
let offset = await self.scrollView.contentOffset
try Task.checkCancellation()
// encode/write...
}

@shadowfacts @libei @mattiem I think that could still get you a bug where the latest offset gets overwritten by an older one as described here: https://mastodon.social/@jjoelson/112286508308952025

I’d be interested in seeing Matt’s nonisolated async method solution 👀

@jjoelson @shadowfacts @libei It’s functionally equivalent to the detached task, nothing fancy. But you now inspiring me to try to cook up a new recipe!