i have a stupid problem. i need to detect changes ...
# codereview
t
i have a stupid problem. i need to detect changes to an object which i do not own. my code looks like this:
Copy code
private fun changeListener(obj: Any) {
    fun getObjectString(): String {
        val baos = ByteArrayOutputStream()
        ObjectOutputStream(baos).also { it.writeObject(obj) }.also { it.close() }
        return baos.toString()
    }

    var oldValue = getObjectString()
    Thread {
        while (true) {
            val newValue = getObjectString()
            if (oldValue != newValue) {
                println("Change detected")
                oldValue = newValue
            }
            Thread.sleep(250)
        }
    }.start()
}
f
@tipsy You can achieve it with
Delegates.observable()
Copy code
var name: String by Delegates.observable(_name) { _, oldName, newName ->
        println("Changed from $oldName to $newName")
        _name = newName
    }
t
thanks @furkan.akdemir - unfortunately it's a java object that already exists. somewhere in the codebase there is a
MyObject myObject = ...
i have access to
myObject
and want to know if it changes
d
Not a solution but it's bothering me 😅.
ObjectOutputStream(baos).also { it.writeObject(obj) }.also { it.close() }
->
ObjectOutputStream(baos).use { it.writeObject(obj) }
.
👍 2
😅 1