【问题标题】:RxJava How to return more than 2 ArrayLists within Observable in a functionRxJava 如何在函数中的 Observable 中返回超过 2 个 ArrayList
【发布时间】:2021-09-29 10:20:59
【问题描述】:

我可以通过 observable 传递两个列表,如下所述

fun loadAll(contact: String?): Single<Pair<ArrayList<Contact>, ArrayList<Contact>>> {
    return packageDB.loadAll()
        .map { table ->
            val one = ArrayList< Contact >()
            val two = ArrayList< Contact >()
            val three = ArrayList< Contact >()
            
            one to two
        }
} 

在 Fragment 中,我试图返回:

  disposables.add(
        viewModel.loadAll(contact)
            .subscribe({
                val firstList = it.first
                val secondList = it.second
                val third = ArrayList<Contact>()
 }))
              

我是响应式编程的新手,所以我无法理解如何传递或返回两个以上的列表,因为 Pair 对象只能有两个子对象。如果您能告诉我解决方案,我将不胜感激。

【问题讨论】:

  • 你不能只创建一个类来保存你想要的任何东西并返回它吗?
  • 同意@IvanWooll。创建一个包含您想要的任何数据的类并在 Single. 中使用它

标签: android observable rx-java


【解决方案1】:

这样做的方法不止一种,我可以列出一些。您只需要考虑返回对象。如果您只想返回 3 件事,如果您清楚自己在做什么,我什至不会费心创建一个特定的类。 Kotlin 已经为此开设了一个课程 - Triple。它就像一对,但包含 3 个值:

fun loadAll(contact: String?): Single<Triple<ArrayList<Contact>, ArrayList<Contact>, ArrayList<Contact>>> {
    return packageDB.loadAll()
        .map { table ->
            val one = ArrayList< Contact >()
            val two = ArrayList< Contact >()
            val three = ArrayList< Contact >()
            
            Triple(one, two, three)
        }
} 

然后以如下方式访问它们:

disposables.add(
        viewModel.loadAll(contact)
            .subscribe({
                val firstList = it.first
                val secondList = it.second
                val third = it.third
 }))

如果你有超过 3 个值,我不确定是否已经有一个类,但总是可以选择使用数组的数组:

fun loadAll(contact: String?): Single<Array<ArrayList<Contact>> {
    return packageDB.loadAll()
        .map { table ->
            val one = ArrayList< Contact >()
            val two = ArrayList< Contact >()
            val three = ArrayList< Contact >()
            
            arrayListOf(one, two, three)
        }
} 


disposables.add(
        viewModel.loadAll(contact)
            .subscribe({ (firList, secondList, thirdList) ->
                  // the above destructures the list into 3 variables
                  // if the list has less than 3 elements you'll get an index out of bounds exception
                  // otherwise you can just use the variables
 }))

正如 cmets 中所指出的,这可能会在理解每个值是什么时产生一些问题,因此有时最好创建自己的类:

data class Values(
   val primaryContacts: ArrayList< Contact >(),
   val secondaryContacts: ArrayList< Contact >(),
   val emergencyContacts: ArrayList< Contact >(),
)

fun loadAll(contact: String?): Single<Values> {
    return packageDB.loadAll()
        .map { table ->
            val one = ArrayList< Contact >()
            val two = ArrayList< Contact >()
            val three = ArrayList< Contact >()
            
            Values(one, two, three)
        }
} 

disposables.add(
        viewModel.loadAll(contact)
            .subscribe({
                val firstList = it.primaryContacts
                val secondList = it.secondaryContacts
                val third = it.emergencyContacts
 }))

请原谅命名。这只是一个例子。

【讨论】:

  • 谢谢伙计。我昨天也是这样解决的。抱歉,我是 Rx 的新手,所以需要时间来理解。
  • 无需道歉。在 StackOverflow 之前我们都去过那里,我们中的一些人:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 2018-08-09
  • 2019-10-13
  • 1970-01-01
相关资源
最近更新 更多