I'm trying to integrate with `NSObject` observabil...
# kotlin-native
l
I'm trying to integrate with
NSObject
observability, but I'm struggling to understand how this Objective-C method signature translates to Kotlin: https://developer.apple.com/documentation/objectivec/nsobject/1416553-observevalueforkeypath?language=objc It seems I need to declare it in an
NSObject
subclass for
addObserver
to work: https://developer.apple.com/documentation/objectivec/nsobject/1412787-addobserver?language=objc Can anyone help me?
b
Yep. KVO is for NSObject subclasses. If you're in the Obj-C world though, nearly everything should inherit from NSObject.
l
What would the
id
type translate to in Kotlin? I see it's used twice, in the type of
ofObject
, and in the type parameter of
change
.
b
id translates roughly to Any
πŸ‘ 1
πŸ™ 1
I would recommend not using KVO though, if you can use an Rx library instead
l
I'd want to make a
suspend
and
Flow
library instead πŸ˜‰
Right now, I'm trying to see what key changed in
NSUserDefaults
to fire a listener that is used to emit to a
Flow
b
Ah okay πŸ‘
l
Here's what I'm coming with:
Copy code
fun observeValueForKeyPath(
    keyPath: String,
    ofObject: Any?,
    change: NSDictionary?,
    context: CPointerVar<*>?
) {
    TODO("Should crash :D")
}
not sure yet if
CPointerVar<*>?
is the correct translation of
void *
which I now see Swift calls
UnsafeMutableRawPointer?
.
b
It's a pretty confusing API, basically you're suppose to set some kind of context pointer which you inspect to know whether or not this particular call is for you (by checking the pointer'a identity) or some other super class in your hierarchy. If the call isn't for you, you call super
You set the context pointer when adding the observer
Swift added a nice wrapper for this API to make it a lot less bad. In any case, welcome to the world of KVO foot guns πŸ˜†
l
Is that API open source?
b
The KVO API? Or the Swift wrapper?
Unsure about either actually. If it is, it's probably somewhere in github.com/apple/Swift
l
The Swift wrapper. Making a foot fun is easy enough πŸ˜‚
b
b
Ah yep that looks right! πŸŽ‰
l
It doesn't seem too hard to understand although I'll
go to
sleep and work on Android stuff tomorrow before resuming Obj-C
fun
(wow, so many coroutines puns there)
b
Yep good time to
suspend
this exploration
l
This night, I could use Obj-C interop successfully, which will allow me to use
dealloc
. But the fact that it can run on any thread doesn't make the things easier with Kotlin/Native way of doing things.
resumeWithException(TooHardException("Swift solution is so complicated!"))
For reference, here's the commit where there's a KVO wrapper (in Obj-C, inspired by Swift implementation) that can be used in Kotlin/Native. That was my first Obj-C experience, and I hope it'll be the last one πŸ˜† Was still interesting to learn about, thanks for the article you linked and the hints! https://github.com/LouisCAD/Splitties/commit/daaa98e5de78a2cd831096b780bdd283d23ba47e
173 Views