【问题标题】:How to return a Maybe or result from Maybe如何从 Maybe 返回 Maybe 或 result
【发布时间】:2019-07-08 15:41:09
【问题描述】:

我在一个用例上,我有一个存储库,其中有我必须做的所有方法,例如:

class MyUseCase(
    private val myRepository: MyRepository
) {

    fun execute(example: String) { //First I don't know what to return here
    //Here I have to check if theres anything on the room db, for this I have this method
    myRepository.findLocally(example) // it returns a Maybe<MyObject>
    //Then if it returns an object I have to do another call that is 
    myRepository.findObject(example) //it will return a Maybe<List<OtherObject>>
    //So if we arrive to this, the usecase is done and I should return the last result.
    //if user doesn't search it before then I have to do an api call using
    myRepository.getFromApi(example) //it will return Single<List<OtherObject>>
    //When I get the result then I have to do two inserts using 
    myRepository.insertWord(example) : Completable
    //And then I want to add all the result from the api call it can be a List<MyObject> or a []
    myRepository.insertList(resultFromApi) : Completable

    }

}

我的问题是我现在知道如何执行所有这些,或者我是否应该拆分它或其他什么,这是从我的演示者那里调用的,所以我想将数据返回给我的演示者以调用 view.showData( ) 例如。

这个想法,应该这样执行:

if(myRepository.findLocally(example)) {
  return myRepository.findObject(example)
}
else{
  myRepository.getFromApi(example)
  myRepository.insertWord(example)
  myRepository.insertList(resultFromApi)
}


编辑

我正在寻找使用 RxJava 的方法。

这就是我正在尝试的方式:

return myRepository.findLocally(example).flatMap {
            myRepository.findObject(example)
        }.switchIfEmpty {
            myRepository.getFromApi(example).flatMap { example ->
                example.flatMap {
                    myRepository.insertList(
                        MyExample(
                            it.id,
                            it.name,
                            it.description
                        )
                    )
                }
                myRepository.insertWord(example)

            }
        }

【问题讨论】:

    标签: android kotlin rx-java


    【解决方案1】:

    方法一:
    你可以返回Any?对象

    private val myRepository() : Any? {
        // Your stuff here
    }
    

    Any 对象基本上意味着你可以返回任何你想要的东西,但要小心检查你的父方法中的返回。
    ? 表示您还可以返回 null 对象。在你的父方法中也检查一下。

    方法二:
    或者使用ifelses 从调用此方法的方法中进行所有这些调用

    方法三:
    另一种方法是使用“指针”返回两者:

    private val myRepository(resul1 : Object1, result2 : Object2, ...) {
        if (function_to_get_result1_fails)
            result1 = Object1() // You set result1 as a basic empty object of type Object1
        else if (function_to_get_result2_fails)
            result2 = Object2()
        // etc.
    }
    

    然后,在您的父方法中,进行如下检查:

    myRepository(object1, object2, ...)
    if (object1.someDataInObject1 != -1) // Set an un-instancied variable as -1 to know if the object is empty or not
        doSomething()
    else if (object2.someDataInObject2 != -1)
        doSomethingElse()
    // etc.
    

    不过,还是更喜欢方法1,或者方法2,但是方法3有点“脏”,白白消耗大量资源。

    【讨论】:

    • 但是我需要使用 rxjava 对吗?如果我调用这些调用,我必须在不同的线程上运行它们......
    • 如你所愿,我猜你的第 2 个电话是依赖的,所以要拨打电话 2,你需要等待电话 1 完成。尝试阅读android中的同步/异步任务
    • 你知道如何使用 RxJava 做到这一点吗?并等待每个电话?
    • 此链接提供有关“阻止”呼叫的信息,这意味着一个呼叫将阻止另一个呼叫,直到它未完成。我认为这是您第 2 次通话所需要的。然后,您可以将此与异步调用结合起来进行其他调用。 code.luasoftware.com/tutorials/android/…
    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多