0

I am using the following code in my function such that it's a part of an Rxjava code where I subscribe. Is there a way I can convert this code to a lambda expression?

 object : Mysubscriber<MyEntry>() {
                                var result: MYEntry? = null

                                override fun onComplete() {
                                    result?.let {
                                        val something = Something(title = it.getField<String>(MyConstants.TITLE_KEY),
                                                somethingImageAsset = it.getField<MyAsset>(MyConstants.IMAGE_KEY),
                                                bodyText = it.getField<String>(MyConstants.BODY_TEXT_KEY))
                                        view?.updateMySpace(something)
                                    }
                                }

                                override fun onError(e: Throwable) {
                                    Timber.e(e)
                                }

                                override fun onNext(entry: MyEntry) {
                                    result = entry
                                }
                            }

Just FYI, the RXjava function for the above code is :

myvar.observe(MyEntry::class.java)
                .one(entryId)
                .applySchedulers()
                .applyOpBeforeAfter(showProgress, hideProgress)
                .subscribe(
                  "above code" )

Any ideas how to easily convert it to lambda expression?

Thanks!

2 Answers2

1

If you are using Kotlin you can do

    val compositeDisposable = CompositeDisposable()
    var result: MYEntry? = null

    val disposable = myvar.observe(MyEntry::class.java)
                    .one(entryId)
                    .applySchedulers()
                    .applyOpBeforeAfter(showProgress, hideProgress)
                    .subscribeBy(
                       onNext = {
                           result = it 
                       },
                       onError = {
                           Timber.e(e)
                       },
                       onComplete = {
                           result?.let {
                                        val something = Something(title = it.getField<String>(MyConstants.TITLE_KEY),
                                                somethingImageAsset = it.getField<MyAsset>(MyConstants.IMAGE_KEY),
                                                bodyText = it.getField<String>(MyConstants.BODY_TEXT_KEY))
                                        view?.updateMySpace(something)
                                    }
                       }
                    )
    compositeDisposable.add(disposable)
    compositeDisposable.dispose() // Dispose it in onStop() or onDestroy()

OR

val compositeDisposable = CompositeDisposable();
var result: MYEntry? = null

val disposable = myvar.observe(MyEntry::class.java)
                        .one(entryId)
                        .applySchedulers()
                        .applyOpBeforeAfter(showProgress, hideProgress)
                        .subscribe(
                           {
                               result = it 
                           },
                           {
                            Timber.e(e)   
                           },
                           {
                               result?.let {
                                            val something = Something(title = it.getField<String>(MyConstants.TITLE_KEY),
                                                    somethingImageAsset = it.getField<MyAsset>(MyConstants.IMAGE_KEY),
                                                    bodyText = it.getField<String>(MyConstants.BODY_TEXT_KEY))
                                            view?.updateMySpace(something)
                                        }
                           }
                        )
compositeDisposable.add(disposable)
compositeDisposable.dispose() // Dispose it in onStop() or onDestroy()

For CompositeDisposable:

private var compositeDisposable = CompositeDisposable()

    fun getCompositeDisposable(): CompositeDisposable {
        if (compositeDisposable.isDisposed)
            compositeDisposable = CompositeDisposable()
        return compositeDisposable
    }

    fun addDisposable(disposable: Disposable) {
        getCompositeDisposable().add(disposable)
    }

    fun dispose() {
        getCompositeDisposable().dispose()
    }
piet.t
  • 11,718
  • 21
  • 43
  • 52
Prithvi Bhola
  • 3,041
  • 1
  • 16
  • 32
  • Where did you declare `result`? – Maroš Šeleng Nov 27 '18 at 12:35
  • `result` is just an object. Please check the answer. – Prithvi Bhola Nov 27 '18 at 13:20
  • Now it looks better. I was just saying that in your original answer, you were using an undeclared variable. Moreover, `subscribeBy` is an extensions function from `RxKotlin` thus it is not present in "pure" `RxJava`. – Maroš Šeleng Nov 27 '18 at 13:46
  • I get a warning that result of subscribeby is not used? cant this be done with existing .subscribe instead of subscribeby? will there be any difference –  Nov 27 '18 at 16:31
  • @MarissaNicholas `result of subscribeby is not used` is coming because you also need to dispose the `observable`. Check this https://stackoverflow.com/questions/49522619/the-result-of-subscribe-is-not-used for more detail. And I have updated the answer with `subscribe` also. Please check. – Prithvi Bhola Nov 27 '18 at 16:55
  • i am just using subscribe from your answer and it seems to be syntactically correct . also using that in kotlin, will there be any issues? –  Nov 27 '18 at 21:52
  • crashes with a nullpointerexception at compositeDisposable!!.add(disposable) –  Nov 27 '18 at 21:54
  • @MarissaNicholas `subscribe` will work fine in `kotlin`. There won't be any issues. For `nullpointerexception ` please check that `compositeDisposable` is initialized properly. Please refer to some documentation/example on who to dispose an observable. You can also refer to this article https://medium.com/@vanniktech/rxjava-2-disposable-under-the-hood-f842d2373e64 – Prithvi Bhola Nov 28 '18 at 03:06
  • I initialize it as : val compositeDisposable:CompositeDisposable ?=null is that right? –  Nov 28 '18 at 03:18
  • @MarissaNicholas You have to initialize it as `val compositeDisposable:CompositeDisposable? = null` then `compositeDisposable = CompositeDisposable()` – Prithvi Bhola Nov 28 '18 at 05:52
  • thanks! I will try it out in a few hours and let you know! –  Nov 28 '18 at 11:12
  • also, do you happen to have any idea about : https://stackoverflow.com/questions/53511227/how-to-avoid-memory-leaks-due-to-custom-static-handler-class?noredirect=1#comment93900039_53511227 –  Nov 28 '18 at 15:55
  • Thank you. Glad it helped :) I will check on another question and revert over that thread. – Prithvi Bhola Nov 28 '18 at 16:31
0

You can achieve this by pressing Alt + Enter. The rest of the work is done by IntelliJ.