Loe
06/01/2023, 3:19 PMFlow
using Swift using KMP-NativeCoroutines library
I have the following flow in my Shared
module:
@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:
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:
pod 'KMPNativeCoroutinesAsync'
pod 'KMPNativeCoroutinesCombine' # Combine implementation
pod 'KMPNativeCoroutinesRxSwift' # RxSwift implementation
Rick Clephas
06/01/2023, 5:38 PMKMPNativeCoroutinesAsync
can actually be imported without compilation errors?
Also could you please check the return type of dailyLogs
in Swift?Loe
06/01/2023, 5:58 PMpod '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:Loe
06/01/2023, 5:58 PMdailyLogs
is a variable I crated in Kotlin:
val dailyLogs: StateFlow<List<DailyLog?>> get() = _dailyLogs
private val _dailyLogs = MutableStateFlow<List<DailyLog?>>(emptyList())
Loe
06/01/2023, 5:59 PMdailyLogs
in Swift?Loe
06/01/2023, 6:06 PMDailyLog
:
@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
)
Rick Clephas
06/01/2023, 6:34 PM@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.Loe
06/01/2023, 7:36 PMLoe
10/15/2024, 3:41 PMv1.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❓
Generic parameter 'Output' could not be inferred
this is my iOS Code snippet:
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:
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
)
Loe
10/15/2024, 5:00 PMRick Clephas
10/15/2024, 5:09 PMLoe
10/15/2024, 6:05 PM