Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@NativeCoroutinesState and StateFlow<Double> #189

Open
sailquilt opened this issue Sep 13, 2024 · 1 comment
Open

@NativeCoroutinesState and StateFlow<Double> #189

sailquilt opened this issue Sep 13, 2024 · 1 comment

Comments

@sailquilt
Copy link

Hi, I'm migrating to KMP-NativeCoroutines and things are working really well so far.

I'm a bit confused on how to use @NativeCoroutinesState with StateFlow.

I have a Kotlin class with the following members:

    private val _foo = MutableStateFlow<Double>(1.0)
    
    @NativeCoroutinesState
    val foo = _foo.asStateFlow()

in my Swift view model I have the following:

        let myFoo: Double = node.foo // this works
        createPublisher(for: node.fooFlow)
            .sink(
                receiveCompletion: { completion in },
                receiveValue: { newValue in
                    let myFoo2: Double = newValue // this fails with error: Cannot convert value of type 'KotlinDouble' to specified type 'Double'
                }
            )
            .store(in: &cancellables)

I was expecting node.fooFlow to give me a type Double, not KotlinDouble.

Also, I'm not sure how to use @NativeCoroutinesRefinedState. If I try using @NativeCoroutinesRefinedState in the above code the member is simply removed.

@rickclephas
Copy link
Owner

Hi!

I was expecting node.fooFlow to give me a type Double, not KotlinDouble.

Yeah there are some edge cases in the Kotlin-ObjC interop when used with callbacks (which are being generated by KMP-NativeCoroutines).
You could indeed make this a little nicer with @NativeCoroutinesRefinedState.

Also, I'm not sure how to use @NativeCoroutinesRefinedState. If I try using @NativeCoroutinesRefinedState in the above code the member is simply removed.

Correct, the refined annotations effectively hide the declarations from autocomplete.
The declarations still exist, but are prefixed with __. Something like the following should work:

extension MyClass {
    var foo: Double { 
        self.__foo
    }
    var fooPublisher: AnyPublisher<Double, Error> {
        createPublisher(for: self.__fooFlow).map { $0.doubleValue }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants