Skip to content

Avoiding sending actions using the 'send' argument from 'Effect.run' after the effect has completed. #3551

Answered by stephencelis
cherologist asked this question in Q&A
Discussion options

You must be logged in to vote

You'll want your MotionManager to introduce a lifecycle for delivering events, either with a Combine publisher or an async sequence.

An AsyncStream is probably the most convenient way to wrap things. For example, just for acceleration updates:

@preconcurrency import Combine

AsyncThrowingStream<CMAccelerometerData, any Error> { continuation in
  let manager = CMMotionManager()
  manager.startAccelerometerUpdates(to: OperationQueue()) { data, error in
    if let error {
      continuation.finish(throwing: error)
    } else if let data {
      continuation.yield(data)
    }
  }
  continuation.onTermination = { _ in
    manager.stopAccelerometerUpdates()
  }
}

And then your dependency can re…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@cherologist
Comment options

Answer selected by cherologist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants