`question` as now KMM will generate objc code for ...
# multiplatform
g
question
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 )
k
A lot of people use https://github.com/rickclephas/KMP-NativeCoroutines#combine to handle converting Flows to Combine
In our project we also use something like this
Copy code
func 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()
}
Copy code
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),
)
g
thank you so much !