Hi , I'm facing an issue regarding `Companion` + `...
# multiplatform
a
Hi , I'm facing an issue regarding
Companion
+
Coroutines
+
iOS
and callback functions I'm trying to call a static function inside a callback,
Copy code
print("Shared object: \(Greeting.Companion.shared)")
print("start as \(Greeting.Companion.shared.getCount())")

Greeting.Companion.shared.addCount()

Greeting.Companion.shared.onCallBack {
	print("Shared object: callback \(Greeting.Companion.shared)")
	print("Should be 1 and it is: \(Greeting.Companion.shared.getCount())")
}
the output is as follow
Copy code
Shared object: com.example.testcompanion.Greeting.Companion@fcc011d0
start as 0
Shared object: callback com.example.testcompanion.Greeting.Companion@fd401110
Should be 1 and it is: 0
onCallBack
starts a coroutine then run the block of code provided , The issue is Companion object has changed and ofc all it's private members I've created a sample project , Any Idea to solve this / workaround , or should i report an issue ?
r
You have a
@ThreadLocal
annotation on your companion. That means that every thread gets a different version of the object. You're incrementing count from the calling thread but reading it on a thread from
Dispatchers.Default
. Remove that annotation. You shouldn't need it unless you're using the legacy memory manager.
a
oh ! yes that fixed it , the lint was complaining about it so i though i did the right thing, Thanks
r
yeah that warning is safe to ignore. "With old Native GC" doesn't apply to you unless you're on an old Kotlin version or explicitly turned it on.