如何将 Observable 中的值返回给 RxJava 2。

huangapple go评论86阅读模式
英文:

How to return value from Observable to Rxjava 2

问题

我遇到了一个问题即 onNext 不能包含 return但我需要返回这个字符串
请求是使用 Retrofit 进行的接口内部有一个工厂 (ApiService)

fun getNameAnimal(name: String): String {
   var nameAnimal = " "

   val api = ApiService.create()

   api.getAnimal("Cat")
   .subscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread())
   .subscribe(
       { animal ->
             // 这部分有效
             Log.i(LOG, animal.name)
             // 这部分无效(值为空)
             nameAnimal = animal.name
       },
       { error ->
             Log.e(LOG, error.printStackTrace())
       }
   )
   return nameAnimal
}

在日志中答案以我需要的格式出现

这个方法位于一个不是 activity 或 fragment 的类中
我该如何实现我的计划
英文:

I ran into a problem that onNext cannot contain return, but I need to return the string.
The request is made using Retrofit with a factory inside the interface (ApiService).

    fun getNameAnimal(name : String) : String {
       var nameAnimal = " "

       val api = ApiService.create()

       api.getAnimal("Cat")
       .subscribeOn(Schedulers.io())
       .observeOn(AndroidSchedulers.mainThread())
       .subscribe(
           { animal -> 
                 // It works
                 Log.i(LOG, animal.name) 
                 // It NOT works (empty value)
                 nameAnimal = animal.name
           },
           { error ->
                 Log.e(LOG, error.printStackTrace())
           }
       )
       return nameAnimal
    }

In the logs, the answer comes in the format I need.

The method is in a class that is not an activity or fragment.
How can I implement my plan?

答案1

得分: 1

fun getNameAnimal(name: String): Single<String> {
    val api = ApiService.create()

    return api.getAnimal("Cat")
        .map { animal -> animal.name }
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
}

在活动或片段中:

apiWorkingClassInstance.getNameAnimal()
    .subscribe(
       { animalName ->
             Log.i(LOG, animalName)
             //todo
       },
       { error ->
             Log.e(LOG, error.printStackTrace())
       }
   )

感谢 Alex_Skvortsov 的建议。

英文:
fun getNameAnimal(name : String) : Single&lt;String&gt; {
   val api = ApiService.create()

   return api.getAnimal(&quot;Cat&quot;)
   .map { animal -&gt; animal.name }
   .subscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread())
}
  1. In activity or fragment

    apiWorkingClassInstance.getNameAnimal()
    .subscribe(
    { animalName ->
    Log.i(LOG, animalName)
    //todo
    },
    { error ->
    Log.e(LOG, error.printStackTrace())
    }
    )

Thanks for the tip Alex_Skvortsov

答案2

得分: 0

以下是翻译好的部分:

一个简单且清晰的方法来实现这一点是使用接口。

  1. 创建你的接口
interface AnimalCallbacks {
  fun onAnimalNameReturned(name: String)
}
  1. 让你的类实现这个接口
class MyAnimal: AnimalCallbacks {
    override fun onAnimalNameReturned(name: String) {
        // 在这里处理逻辑
    }
}

从这里开始,你可以在方法调用中传递你的接口,并使用接口中定义的方法发送结果回来。

fun getNameAnimal(name: String, callbacks: AnimalCallbacks) {
    var nameAnimal = ""

    val api = ApiService.create()

    api.getAnimal("Cat")
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            { animal ->
                // 这部分工作正常
                Log.i(LOG, animal.name)
                // 这部分不起作用(值为空)
                nameAnimal = animal.name
                callbacks.onAnimalNameReturned(animal.name)
            },
            { error ->
                Log.e(LOG, error.printStackTrace())
            }
        )
}
英文:

An easy and clean way to achieve this would be to use an interface.

  1. Create your interface
    interface AnimalCallbacks {
      fun onAnimalNameReturned(name: String)
    }
  1. Have your class implement the interface
    class MyAnimal: AnimalCallbacks {
    
            override fun onAnimalNameReturned(name: String) {
                //handle logic here
            }
        }

From here you can pass your interface in the method call and send the result back using the method you defined in the interface.

 fun getNameAnimal(name : String, callbacks: AnimalCallbacks) {
        var nameAnimal = &quot; &quot;

        val api = ApiService.create()

        api.getAnimal(&quot;Cat&quot;)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                { animal -&gt;
                    // It works
                    Log.i(LOG, animal.name)
                    // It NOT works (empty value)
                    nameAnimal = animal.name
                    callbacks.onAnimalNameReturned(animal.name)
                },
                { error -&gt;
                    Log.e(LOG, error.printStackTrace())
                }
            )
    }

huangapple
  • 本文由 发表于 2020年3月16日 19:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/60704730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定