1

still very new to Kotlin, i'm trying not to call my callback if my activity is null. Let me explain, i have my DataManager that perform async work on some data:

fun performWork(callback: ((param1: T, param2) -> Unit)?) { 
   // ... async work with data... 
   // Work is finished, let's call the callback :) 
   // as the callback is optional: 
   callback?.invoke(param1, param2)
}

So i call my DataManager into my MainActivity:

DataManager.performWork(callback = { param1, param2 -> 
  // update the UI now that the work is done. 
}

What i want to do is not to call the callback if the callback is null (which means activity is null, or not on screen anymore...)

So in my activity i declare an optional var:

var myCallback: ((params1: T, params2: T) -> Unit)? = null

Then i have a function:

  var callbackMain: ((params1: T, params2: T) -> Unit)? = fun (params1: T, params2: T) {

        print("ok")

    }

Then when i call my DataManager:

    mainCallback = this.callbackMain
    DataManager.performWork(mainCallback)
    mainCallback = null

So the async work is called... and the DataManager enter the callback because mainCallback is not null !!! I think its value is callbackMain. Is there anyway i can make my callback nullable ? So it won't be called, and that my DataManager does not know the activity ?

Thank you very much for any help.

user2434385
  • 281
  • 1
  • 3
  • 16
  • How do you access the Context from within the callback? You could make it access a `private var context: Context?` which you null out in `onPause()` or wherever. – Marko Topolnik Apr 23 '18 at 14:33
  • There is no access on the context for now. you think i should pass the context ? – user2434385 Apr 23 '18 at 14:37
  • You shouldn't pass it as a parameter to the callback. The callback must try to access it from the property you can null out, so the callback knows whether or not to go on. – Marko Topolnik Apr 23 '18 at 14:41
  • Ok, i have made a var into the DM, and on my activity i call ```DataManager.callback = this.myFunc```if i set it to null in the activity it won't be called. So passing it as a paramter make it a strong copy ? what is happening, anyway to pass the callback as reference value ? – user2434385 Apr 23 '18 at 14:44
  • This is elementary stuff, same as in Java. If you pass a reference to an object, the reference cannot suddenly become `null`. If your callback captures the instance where you're declaring it, and that instance has a mutable property, then it will observe changes to the property. – Marko Topolnik Apr 23 '18 at 14:48
  • I'm a beginner, does not work like this in other langage :) – user2434385 Apr 23 '18 at 14:51

1 Answers1

0

You transfer your mainCallback as a param of the function performWork. When you do this mainCallback = null , you make your mainCallback to null

however,the param mainCallback in your function will still not null.

What you have to do is injecting the callback into DataManager but not as a param of function. Then when your async work done, then you call mainCallback?.invoke()

If you want to destroy the callback , you just have to set the callback in DataManager into null.

weechan
  • 87
  • 9
  • Thank you. First i can't destroy the callback in DM because it depends on the activity... If the activity still here i want to call it ! What do you mean by "but not as a param of function" (syntax not easy for a beginner) Thank you. – user2434385 Apr 23 '18 at 14:06