Any way to implement `deinit` in Kotlin on iOS? I ...
# kotlin-native
r
Any way to implement
deinit
in Kotlin on iOS? I guess not but you never know
Maybe I can override
finalize
. Yeah no that’s deprecated and not used
l
You need to do it using Objective-C AFAIK. If you get something working reliably, might be worth sharing in a gist or something else.
r
I’m not writing Objective-C, only 100% Kotlin
We’ll see where we can get
l
But deinit is Obj-C and Swift only. I was mentioning Obj-C because you could write a wrapper to call a regular function that can be implemented in Kotlin.
r
I’m just trying to find a way to react to a UIViewController being removed/destroyed/obliterated. Guess I’ll have to wrap my calls causing controllers destruction with something
I think we need a native ios library which would basically just be RxSwift’s DisposeBag: something on which you can set callbacks called on its deinit
s
View controllers give some decent feedback as to when they're not being used. Check out the will move to parent and the did move to parent calls. When the parent is null, it is no longer contained in another view controller. https://developer.apple.com/documentation/uikit/uiviewcontroller/1621381-willmove
r
@Sam that only works for viewcontrollers inside other viewcontrollers actually it doesn’t
l
Isn't there a way to detect a ViewController is the root one?
r
A ViewController no longer having a parent does not mean the same thing anyway. You could be detaching it to attach it to another ViewController
s
If you’re detaching to move to another controller you would pass that other controller to will move instead of null.
r
Would you be able to swap 2 controllers without passing null to either ones this way? I’m not sure it’s a definitive solution
s
Louis, the window object contains the reference to the root view controller. You can also walk up the vc hierarchy by continually asking for parent until you get null. That can lead to a detached vc but will only happen if the vc’s view.window property is null.
Sure. A vc can have more than one child vc. You would remove the views from the view hierarchy first, move the vcs around and then rebuild the view hierarchy to reflect the vc’s new parents.
It might not even be necessary to remove the views first as long as the views get updated right after the vc swap.
344 Views