Gao Hang
06/30/2023, 4:06 AMquestion
as now KMM will generate objc code for iOS. how you guys use the generated model in SwiftUI.
( I thought it’s hard to use as @State and @Binding object )Kevin S
06/30/2023, 2:30 PMKevin S
06/30/2023, 2:31 PMfunc createPublisher<T>(_ flowAdapter: FlowAdapter<T>) -> AnyPublisher<T?, KotlinError> {
return Deferred<Publishers.HandleEvents<PassthroughSubject<T?, KotlinError>>> {
let subject = PassthroughSubject<T?, KotlinError>()
let canceller = flowAdapter.subscribe(
onEach: { item in subject.send(item) },
onComplete: { subject.send(completion: .finished) },
onThrow: { error in subject.send(completion: .failure(KotlinError(error))) }
)
return subject.handleEvents(receiveCancel: { canceller.cancel() })
}.eraseToAnyPublisher()
}
fun subscribe(
onEach: (item: T) -> Unit,
onComplete: () -> Unit,
onThrow: (error: Throwable) -> Unit,
): Canceller = JobCanceller(
flow.onEach { onEach(it) }
.catch { onThrow(it) }
.onCompletion { onComplete() }
.launchIn(scope),
)
Gao Hang
07/11/2023, 9:18 AM