Hi everyone!
I was wondering how you implement listeners in KMM ( in particular K\N)? To avoid retain cycles?
Do you use expect/actual and wrap listener with WeakReference on K\N?
For example:
Copy code
// Multiplatform
class KmmLibrary(private val listener: Listener) {
fun doSomething() {
listener.notifyResult("All good!")
}
interface Listener {
fun notifyResult(result: String)
}
}
// Native
struct ContentView: View {
@State private var message: String = ""
private let kmmLibrary: KmmLibrary
init() {
self.kmmLibrary = KmmLibrary(listener: NativeListener())
}
class NativeListener: Listener {
func notifyResult(result: String) {
print(result)
}
}
}
Does not this code created retain cycle on iOS (Native)?
Should I somehow wrap it with WeakReference using expect/actual mechanism or are there any other common practices?