Hi guys, I’m having an issue collecting `Flow` usi...
# multiplatform
l
Hi guys, I’m having an issue collecting
Flow
using Swift using KMP-NativeCoroutines library I have the following flow in my
Shared
module:
Copy code
@NativeCoroutinesState
override val dailyLogs: StateFlow<List<DailyLog?>> get() = _dailyLogs
private val _dailyLogs = MutableStateFlow<List<DailyLog?>>(emptyList())
in iOS, I tried to collect the flow using:
Copy code
import SwiftUI
import shared
import Foundation
import Combine
import KMPNativeCoroutinesCore
import KMPNativeCoroutinesAsync

func startObservingDatabaseUpdates() async {
    do {
        let stream = asyncSequence(for: dailyLogRepo.dailyLogs)
        for try await data in stream {
            self.logList = data
        }
    } catch {
        print("Failed with error: \(error)")
    }
}
but I am getting this error:
Cannot find 'asyncSequence' in scope
I added the following dependencies in Cocopods:
Copy code
pod 'KMPNativeCoroutinesAsync'
  pod 'KMPNativeCoroutinesCombine'  # Combine implementation
  pod 'KMPNativeCoroutinesRxSwift'  # RxSwift implementation
r
Could you verify that
KMPNativeCoroutinesAsync
can actually be imported without compilation errors? Also could you please check the return type of
dailyLogs
in Swift?
l
I think i made a little bit of progress after specifying the library version explicitly in Cocopods:
Copy code
pod 'KMPNativeCoroutinesAsync', '1.0.0-ALPHA-10'
  pod 'KMPNativeCoroutinesCombine', '1.0.0-ALPHA-10'  # Combine implementation
  pod 'KMPNativeCoroutinesRxSwift', '1.0.0-ALPHA-10'  # RxSwift implementation
Now i’m getting this error:
dailyLogs
is a variable I crated in Kotlin:
Copy code
val dailyLogs: StateFlow<List<DailyLog?>> get() = _dailyLogs
private val _dailyLogs = MutableStateFlow<List<DailyLog?>>(emptyList())
how can I check the return type of
dailyLogs
in Swift?
btw this is the definition of
DailyLog
:
Copy code
@Serializable
data class DailyLog(
    val date: LocalDate,
    val grade: Int,
    val taskList: List<Task>
)

/**
 * data model for task
 */
@Serializable
data class Task(
    val name: String,
    val completed: Int = 0,
    val point: Int = 0
)
r
Alright that new error makes more sense 😄. The
@NativeCoroutinesState
property converts your
StateFlow
property to a regular property that exposes the current value of the StateFlow. To access the Flow itself you would need to use the
dailyLogsFlow
property. Or alternatively use the
@NativeCoroutines
annotation instead.
l
That worked. Thank you so much 🙏
@Rick Clephas I updated KMP-NativeCoroutines to the latest version
v1.0.0-ALPHA-36
now i'm getting the following error. the library worked fine on version
1.0.0-ALPHA-10
, what am i missing with the latest version
Copy code
Generic parameter 'Output' could not be inferred
this is my iOS Code snippet:
Copy code
import KMPNativeCoroutinesAsync

// TODO: getting this error on `latestAppVersionFlow`: Generic parameter 'Output' could not be inferred
let stream = asyncSequence(for: appVersionRepo.latestAppVersionFlow)
for try await appVersion in stream {
    DispatchQueue.main.async {
        . . .
    }
}
this is my Kotlin code:
Copy code
import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable

object AppVersionRepo : AppVersionRepoI {
    private val appVersionRemoteDataSource: AppVersionRemoteDataSourceI = AppVersionRemoteDataSource()
    private val dispatcherScope = CoroutineScope(Dispatchers.Default)

    @NativeCoroutinesState
    override val latestAppVersion: StateFlow<String?> get() = _latestAppVersion
    private val _latestAppVersion = MutableStateFlow<String?>(null)

    init {
        dispatcherScope.launch {
            appVersionListener()
        }
    }

    private suspend fun appVersionListener() {
        appVersionRemoteDataSource.latestAppVersion
            .collect { appVersionDataSnapshot ->
                val appVersion = appVersionDataSnapshot.value<AppVersion>()
                _latestAppVersion.value = appVersion.latestAppVersion
            }
    }
}

interface AppVersionRepoI {
    @NativeCoroutinesState
    val latestAppVersion: StateFlow<String?>
}

@Serializable
data class AppVersion(
    val latestAppVersion: String
)
r
👍 1
l
got it! Thanks again 🙏