Osman Saral
12/04/2023, 6:27 PMKMP-NativeCoroutines
library and I'm getting Escaping closure captures mutating 'self' parameter
error.
The shared ViewModel has this:
private val _incomingMessage = MutableSharedFlow<Incoming>(10)
@NativeCoroutines
val incomingMessage = _incomingMessage.asSharedFlow()
Any idea what's wrong?Rick Clephas
12/04/2023, 6:34 PMself
and mainViewModel
.Osman Saral
12/04/2023, 6:37 PM@main
struct MyApp: App {
let mainViewModel: MainViewModel = inject()
init() {
print("Starting app")
KoinApplication.start()
let handle = Task {
do {
let sequence = asyncSequence(for: self.mainViewModel.incomingMessage)
for try await message in sequence {
print("Message: \(message)")
}
} catch {
print("Failed with error: \(error)")
}
}
}
}
func inject<T : AnyObject>() -> T {
return KoinApplication.shared.koin.get(objCClass: T.self) as! T
}
typealias KoinApplication = Koin_coreKoinApplication
extension KoinApplication {
static let shared = KoinIOSKt.doInitKoinIos()
@discardableResult
static func start() -> KoinApplication {
shared
}
}
Rick Clephas
12/04/2023, 6:49 PMinit
being mutating. Adding [self]
should fix it:
let handle = Task { [self] in
sourceOsman Saral
12/04/2023, 6:55 PMRick Clephas
12/04/2023, 7:00 PMinit
of the MyApp
struct. Inside the init self
is mutating which is captured by the Tasks escaping closure.
Per the comment: that is and error by default, unless explicitly specifying the captured self (which makes self immutable).Osman Saral
12/04/2023, 7:03 PMinit
s are mutating. Got it, thank you