{"id":71163,"date":"2023-12-29T02:10:05","date_gmt":"2023-12-28T18:10:05","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=71163"},"modified":"2023-12-29T02:10:05","modified_gmt":"2023-12-28T18:10:05","slug":"kotlin-rxjava-multidata","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/kotlin-rxjava-multidata.html","title":{"rendered":"Kotlin \u4e0b \u7684RxJava \u591a\u6570\u636e\u6e90\u5904\u7406"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u4e3b\u8981\u8bb2\u89e3Kotlin\u8bed\u8a00\u5904\u7406RxJava\u591a\u6570\u636e\u6e90\u7684\u5b9e\u73b0\u3002\u9605\u8bfb\u672c\u6587\u6700\u597d\u6709Kotlin<\/a>\u57fa\u7840\uff0c\u82e5\u6ca1\u6709\u57fa\u7840\uff0c\u53ef\u53c2\u8003\u4e4b\u524d\u6587\u7ae0Kotlin\u521d\u63a2<\/a>\uff0c\u4f7f\u7528Kotlin\u4f18\u96c5\u7684\u5f00\u53d1Android\u5e94\u7528<\/a>\uff0c\u4ee5\u53caRxJava\u57fa\u7840\uff08\u672c\u6587\u57fa\u4e8eRxJava2\uff09\uff0c\u5f53\u7136\u6211\u4e5f\u4f1a\u5c3d\u53ef\u80fd\u8be6\u7ec6\u89e3\u91ca\u8ba9\u4f60\u987a\u5229\u9605\u8bfb\u672c\u6587\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6e90\u7801\u4f20\u9001\u95e8<\/a><\/p>\n

\u5199\u5728\u524d\u9762<\/strong><\/div>\n

\u6700\u8fd1\u51e0\u5929\u56de\u8fc7\u5934\uff0c\u770b\u4e86\u4e4b\u524d\u7684\u603b\u7ed3RxJava\u64cd\u4f5c\u7b26\u7cfb\u5217<\/a>\uff0c\u611f\u89c9\u5bf9Rxjava\u591a\u6570\u636e\u6e90\u7684\u5904\u7406\u4e0d\u662f\u5f88\u7406\u89e3\uff0c\u6240\u4ee5\u5728\u603b\u7ed3\u5b66\u4e60\u4e00\u6ce2\u3002\u5927\u5bb6\u90fd\u77e5\u9053\uff0c\u6700\u8fd1Kotlin\u8bed\u8a00\u4e00\u76f4\u5360\u636e\u70ed\u641c\u699c\uff0c\u8912\u8d2c\u4e0d\u4e00\uff0c\u4f46\u6211\u60f3\u8bf4\uff0c\u4e0d\u7ba1\u6709\u4ec0\u4e48\u60f3\u6cd5\u90fd\u8981\u629b\u5728\u8111\u540e\uff0c\u6bd5\u7adfGoogle\u7238\u7238\u51fa\u624b\uff0c\u4f60\u4e0d\u60c5\u613f\u4e5f\u8981\u8ddf\u968f\u5b83\u7684\u6b65\u4f10\u3002\u9274\u4e8e\u6b64\uff0c\u672c\u7bc7\u5bf9RxJava\u591a\u6570\u636e\u6e90\u7684\u603b\u7ed3\u662f\u57fa\u4e8eKotlin\u8bed\u8a00\uff0c\u4e5f\u8ba9\u5927\u5bb6\u660e\u767d\uff0c\u4f7f\u7528Kotlin\u5f00\u53d1\u5e94\u7528\u5e76\u4e0d\u662f\u4e0d\u80fd\u4f7f\u7528Java\u5e93\uff0c\u73b0\u5728\u6709\u4e00\u90e8\u5206\u4eba\u62c5\u5fc3\uff0cKotlin\u7b2c\u4e09\u65b9\u5e93\u90a3\u4e48\u5c11\uff0c\u5982\u679c\u4f7f\u7528Kotlin\u5f00\u53d1\u90a3\u4e0d\u662f\u7ed9\u81ea\u5df1\u627e\u7f6a\u53d7\uff0c\u5176\u5b9e\u4f60\u5b8c\u5168\u9519\u4e86\uff0c\u5f53\u4f60\u8bf4\u8fd9\u8bdd\u7684\u65f6\u5019\uff0c\u6211\u6562\u65ad\u5b9a\u4f60\u90fd\u8fd8\u6ca1\u6709\u63a5\u89e6Kotlin\uff0c\u56e0\u4e3aKoltin\u6709\u4e00\u4e2a\u6700\u91cd\u8981\u7684\u4f18\u52bf\u5c31\u662f\u548cJava\u7edd\u5bf9\u517c\u5bb9\u3002<\/p>\n

\u591a\u6570\u636e\u6e90\u5904\u7406\u64cd\u4f5c\u7b26<\/strong><\/div>\n

\u5728RxJava\u4e2d\u591a\u6570\u636e\u6e90\u5904\u7406\u7684\u64cd\u4f5c\u7b26\u5f88\u591a\uff0c\u4f46\u662f\u6700\u7ecf\u5178\u7684\u5c31\u8981\u6570merge,contact\uff0czip\u4e86\u3002\u5982\u679c\u5bf9\u8fd9\u4e09\u4e2a\u64cd\u4f5c\u7b26\u4e0d\u662f\u5f88\u719f\u6089\u7684\u8bdd\uff0c\u53ef\u4ee5\u53bb\u67e5\u770b\u5b83\u7684\u4f7f\u7528<\/a>\uff0c\u5f53\u7136\u5982\u679c\u4f60\u61d2\u5f97\u53bb\u770b\uff0c\u6211\u4e5f\u4f1a\u7b80\u5355\u63d0\u4e00\u4e0b\u3002merge\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5904\u7406\u591a\u4e2aObservable\u53d1\u9001\u7684\u6570\u636e\uff0c\u5b83\u662f\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u4e0d\u4fdd\u8bc1\u6570\u636e\u53d1\u9001\u7684\u987a\u5e8f\uff0c\u5373\u6709\u53ef\u80fd\u51fa\u73b0\u6570\u636e\u4ea4\u53c9\uff0c\u5f53\u4e00\u4e2aObservable\u53d1\u9001\u4e86onError\u540e\uff0c\u672a\u6267\u884c\u7684Observable\u4e0d\u5728\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u63a5\u6267\u884cmerge\u7684onError\u65b9\u6cd5\u3002<\/p>\n

contact\u64cd\u4f5c\u7b26\u6267\u884c\u65f6\u4e00\u4e2a\u540c\u6b65\u64cd\u4f5c\uff0c\u4e25\u683c\u6309\u7167contact\u4e2d\u4f20\u5165Observable\u5148\u540e\u6267\u884c\uff0c\u5373\u524d\u9762\u7684\u5148\u6267\u884c\u540e\u9762\u7684\u540e\u6267\u884c\uff0c\u5e76\u4e14\u6700\u7ec8\u53d1\u9001\u7684\u6570\u636e\u4e5f\u662f\u6709\u5e8f\u7684\uff0c\u5373\u7b2c\u4e00\u4e2aObservable\u7684\u6570\u636e\u53d1\u9001\u5b8c\u6bd5\u518d\u53d1\u9001\u7b2c\u4e8c\u4e2a\uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002<\/p>\n

zip\u64cd\u4f5c\u7b26\u548ccontact\u548cmerge\u6709\u4e86\u672c\u8d28\u7684\u533a\u522b\uff0c\u5b83\u4f1a\u5c06\u6bcf\u4e2aObservable\u4e2a\u6570\u636e\u9879\u5206\u5e03\u5bf9\u5e94\u8fd4\u56de\u4e00\u4e2aObservable\u518d\u53d1\u9001\uff0c\u6700\u7ec8\u53d1\u9001\u7684\u6570\u636e\u91cf\u4e0e\u6700\u5c0f\u6570\u636e\u957f\u5ea6\u76f8\u540c\u3002<\/p>\n

\u4f7f\u7528\u573a\u666f\u5206\u6790<\/strong><\/div>\n

\u5047\u5982\u73b0\u5728\u6211\u4eec\u6709\u4e09\u79cd\u5546\u54c1\uff0c\u6709\u4e00\u4e2a\u67e5\u8be2\u5546\u54c1\u4fe1\u606f\u7684\u63a5\u53e3\uff0c\u6839\u636e\u63a5\u53e3\u53ef\u4ee5\u67e5\u8be2\u8be5\u5546\u54c1\u7684\u4ef7\u683c\u4ee5\u53ca\u51fa\u552e\u5730\u70b9\u3002\u5546\u54c1\u5b9e\u4f53\u7c7b<\/p>\n

data class Goods(var id:Int,var price: Int, var address: String)<\/pre>\n

\u5728Kotlin\u8bed\u8a00\u4e2d\uff0c\u5b9e\u4f53\u7c7b\u521b\u5efa\u7528data class \u5173\u952e\u8bcd\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u548cJava\u4e00\u6837\u521b\u5efaget\/set\u65b9\u6cd5\uff0c\u53ea\u9700\u4e00\u884c\u4ee3\u7801\u641e\u5b9a\u3002<\/p>\n

\u521b\u5efa\u6a21\u62df\u7f51\u7edc\u8bf7\u6c42<\/p>\n

object NetRequest {\r\n    \/\/\u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\r\n    fun getGoodsObservable(id: Int): Observable<Goods> {\r\nfun getGoodsObservable(id: Int): Observable<Goods> {\r\n        return Observable.create {\r\n            source ->\r\n            Thread.sleep(Random().nextInt(1000).toLong())\r\n            var data = Goods(id, Random().nextInt(20), \"\u5730\u5740${id}\")\r\n            source.onNext(data)\r\n            source.onComplete()\r\n            Log.e(\"getGoodsObservable:\", \"${id}\")\r\n        }\r\n    }\r\n}<\/pre>\n

\u5728\u4e0a\u9762\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5355\u4f8b\u7c7b\uff0c\u5728Kotlin\u4e2d\u4f7f\u7528object\u4fee\u9970\u7c7b\u65f6\u5373\u7ed9\u6211\u4eec\u81ea\u52a8\u521b\u5efa\u4e86\u4e00\u4e2a\u5355\u4f8b\u5bf9\u8c61\u3002\u5728\u6bcf\u4e00\u53e5\u4ee3\u7801\u7ed3\u5c3e\u6211\u4eec\u4e0d\u9700\u8981\u518d\u548cJava\u4e00\u6837\u5199\u4e00\u4e2a\u5206\u53f7\u201c\uff1b\u201d\u6765\u7ed3\u675f\uff0c\u4ec0\u4e48\u4e5f\u4e0d\u7528\u5199\u3002<\/p>\n

Observable.create\u4f7f\u7528\u7684\u662flambda\u8868\u8fbe\u5f0f\uff0c\u5728Kotlin\u8bed\u8a00\u4e2d\u662f\u652f\u6301lambda\u8868\u8fbe\u5f0f\u7684\u3002source \u5c31\u662fObservableEmitter<Goods>\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8c03\u7528onNext\u53d1\u9001\u6570\u636e\u3002\u4e3a\u4e86\u66f4\u51c6\u786e\u7684\u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\uff0c\u4f7f\u7528Thread.sleep\u968f\u673a\u7684\u5ef6\u8fdf\uff0c\u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\u7684\u65f6\u95f4\u3002<\/p>\n

  fun getGoodsObservable(id: Int): Observable<Goods> {\r\n        return Observable.create {\r\n            source ->\r\n            Thread.sleep(Random().nextInt(1000).toLong())\r\n            var data = Goods(id, Random().nextInt(20), \"\u5730\u5740${id}\")\r\n            source.onNext(data)\r\n            source.onComplete()\r\n            Log.e(\"getGoodsObservable:\", \"${id}\")\r\n        }<\/pre>\n

\u5f53\u7136\u7531\u4e8esubscribe\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5199\u3002\u4e5f\u5c31\u662f\u7701\u7565\u4e86source ->\uff0c\u6b64\u65f6it\u5c31\u8868\u793a\u8be5\u53c2\u6570\u6570\u636e\u3002<\/p>\n

return Observable.create {\r\n            Thread.sleep(Random().nextInt(1000).toLong())\r\n            var data = Goods(id, Random().nextInt(20), \"\u5730\u5740${id}\")\r\n            it.onNext(data)\r\n            it.onComplete()\r\n            Log.e(\"getGoodsObservable:\", \"${id}\")\r\n        }<\/pre>\n

\u5728java\u4e2d\u5b9e\u73b0\u5982\u4e0b<\/p>\n

  return Observable.create(new ObservableOnSubscribe<Goods>() {\r\n\r\n            @Override\r\n            public void subscribe(@NonNull ObservableEmitter<Goods> e) throws Exception {\r\n             \/\/\u5904\u7406\u903b\u8f91\r\n            }\r\n        });<\/pre>\n
merge<\/strong><\/span><\/div>\n

\u51c6\u5907\u597d\u4e86\u8bf7\u6c42\u64cd\u4f5c\uff0c\u5f00\u59cb\u4f7f\u7528merge\u770b\u770b\u6267\u884c\u7684\u6548\u679c\u3002<\/p>\n

  fun executeMerge() {\r\n        Observable.merge(getGoodsObservable(1).subscribeOn(Schedulers.newThread()),\r\n                getGoodsObservable(2).subscribeOn(Schedulers.newThread()),\r\n                getGoodsObservable(3).subscribeOn(Schedulers.newThread()))\r\n                .subscribeOn(Schedulers.newThread())\r\n                .observeOn(AndroidSchedulers.mainThread())\r\n                .toList()\r\n                .subscribe({\r\n                    Log.e(TAG, it.toString())\r\n                }, {\r\n                    Log.e(TAG, it.toString())\r\n                })\r\n    }<\/pre>\n

merge\u4e2d\u6709\u4e09\u4e2a\u7f51\u7edc\u8bf7\u6c42\u64cd\u4f5c\uff0c\u5e76\u901a\u8fc7subscribeOn(Schedulers.newThread())\u5c06\u7f51\u7edc\u8bf7\u6c42\u5207\u6362\u5230\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u6570\u636e\u90fd\u8bf7\u6c42\u6210\u529f\u540e\uff0c\u518d\u901a\u8fc7observeOn(AndroidSchedulers.mainThread())\u5207\u6362\u5230\u4e3b\u7ebf\u7a0b\u8bf7\u6c42\u6570\u636e\u3002\u4e3a\u4e86\u4e09\u8bf7\u6c42\u90fd\u6210\u529f\u540e\uff0c\u6211\u4eec\u5728\u66f4\u65b0UI\uff0c\u6240\u4ee5\u901a\u8fc7toList()\u5c06\u8bf7\u6c42\u7684\u6570\u636e\u8f6c\u6362\u6210List\u4e00\u5757\u53d1\u9001\u3002\u5728\u4e0a\u9762\u7684subscribe\u4f9d\u7136\u4f7f\u7528\u7684lambda\u8868\u8fbe\u5f0f\uff0csubscribe\uff08{}\uff0c{}\uff09\u4e2d\u7b2c\u4e00\u4e2a\u62ec\u53f7\u662fonSuccess\u56de\u8c03\uff0c\u91cc\u9762\u7684it\u662f\u63a5\u6536\u5230\u7684List< Goods >\u6570\u636e\uff0c\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u662fonError\u56de\u8c03\uff0cit\u8868\u793a\u5f02\u5e38Throwable\u5bf9\u8c61\u3002<\/p>\n

subscribe\u90e8\u5206Java\u4ee3\u7801<\/p>\n

.subscribe(new Consumer<List<Goods>>() {\r\n                    @Override\r\n                    public void accept(@NonNull List<Goods> goodses) throws Exception {\r\n\r\n                    }\r\n                }, new Consumer<Throwable>() {\r\n                    @Override\r\n                    public void accept(@NonNull Throwable throwable) throws Exception {\r\n\r\n                    }\r\n                });<\/pre>\n

\u5f53\u7136\u5982\u679c\u4f60\u60f3\u4f7f\u7528RxJava2\u4e2donSubscribe(@NonNull Disposable d) \uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528subscribe<\/p>\n

.subscribe(object : SingleObserver<List<Goods>> {\r\n                    override fun onSubscribe(d: Disposable?) {\r\n                    }\r\n                    override fun onError(e: Throwable?) {\r\n                    }\r\n                    override fun onSuccess(t: List<Goods>?) {\r\n                    }\r\n                })<\/pre>\n

\u4e3a\u4e86\u89c2\u5bdf\uff0c\u6211\u4eec\u5c06\u8bf7\u6c42\u6210\u529f\u7684\u6570\u636e\u663e\u793a\u5728\u754c\u9762\u4e0a\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aButton\uff0cTextView\u3002<\/p>\n

class MainActivity : AppCompatActivity(), View.OnClickListener {\r\n\r\n    val TAG = \"MainActivity\"\r\n    override fun onCreate(savedInstanceState: Bundle?) {\r\n        super.onCreate(savedInstanceState)\r\n        setContentView(R.layout.activity_main)\r\n        setSupportActionBar(toolbar)\r\n        \/\/\u52a0\u5165\u8fd9\u53e5import kotlinx.android.synthetic.main.activity_main.*\r\n        \/\/\u4e0d\u7528\u518dfindViewById,\u53ef\u76f4\u63a5\u4f7f\u7528\r\n        merge.setOnClickListener(this)\r\n\r\n    }\r\n    override fun onClick(v: View) {\r\n        when (v.id) {\r\n            R.id.merge -> {\r\n                executeMerge()\r\n            }\r\n        }\r\n        \/\/when \u5173\u952e\u5b57\u548cJava\u4e2d\u7684Switch\u5173\u952e\u8bcd\u662f\u7c7b\u4f3c\u7684\uff0c\r\n        \/\/\u53ea\u4e0d\u8fc7\u5b83\u6bd4Java\u4e2d\u7684Switch\u5f3a\u5927\u7684\u591a\uff0c\u53ef\u4ee5\u63a5\u6536\u4efb\u4f55\u53c2\u6570\uff0c\r\n        \/\/\u7136\u540e\u5224\u65ad\u4f7f\u7528,\u4e5f\u53ef\u4ee5\u5982\u4e0b\u4f7f\u7528\r\n        when (v) {\r\n            merge -> {\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n
contact<\/strong><\/span><\/div>\n

\u6211\u4eec\u70b9\u51fb\u6267\u884c\u51e0\u6b21\u53d1\u73b0\uff0c\u8fd4\u56de\u7684List\u7684\u6570\u636e\u5e76\u4e0d\u662f\u6309\u7167merge\u53c2\u6570\u7684\u5148\u540e\u987a\u5e8f\u6267\u884c\u7684\uff0c\u5b83\u662f\u5e76\u53d1\u7684\uff0c\u6700\u7ec8\u7684\u987a\u5e8f\uff0c\u662f\u7531\u7f51\u7edc\u8bf7\u6c42\u7684\u5feb\u6162\u51b3\u5b9a\u7684\uff0c\u8bf7\u6c42\u8fd4\u56de\u6570\u636e\u8d8a\u5feb\u4e5f\u5c31\u8868\u793a\u8be5\u6570\u636e\u6700\u65e9\u53d1\u9001\uff0c\u5373\u5728List\u4e2d\u6700\u9760\u524d\u3002\u90a3\u4e48\u6b64\u65f6\u51fa\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u6211\u60f3\u8fd4\u56de\u6570\u636e\u7684List\u987a\u5e8f\u4e25\u683c\u6309\u7167\u4f4d\u7f6e\u7684\u5148\u540e\u987a\u5e8f\u5462\uff1f\u90a3\u6b64\u65f6\u4f7f\u7528merge\u7684\u8bdd\uff0c\u662f\u4e0d\u592a\u73b0\u5b9e\u4e86\u3002\u5f53\u7136\u524d\u9762\u6211\u4eec\u63d0\u5230contact\u53ef\u4ee5\u4f7f\u7528\u3002\u90a3\u4e48\u76f4\u63a5\u5c06merge\u66f4\u6539\u4e3acontact\u6267\u884c\u4ee5\u4e0b\u8bd5\u8bd5\uff0c<\/p>\n

    fun executeContact() {\r\n        Observable.concat(getGoodsObservable(1).subscribeOn(Schedulers.newThread()),\r\n                getGoodsObservable(2).subscribeOn(Schedulers.newThread()),\r\n                getGoodsObservable(3).subscribeOn(Schedulers.newThread()))\r\n                .subscribeOn(Schedulers.newThread())\r\n                .observeOn(AndroidSchedulers.mainThread())\r\n                .toList()\r\n                .subscribe({\r\n                    Log.e(TAG, it.toString())\r\n                }, {\r\n                    Log.e(TAG, it.toString())\r\n                })\r\n    }<\/pre>\n

\u7684\u786e\uff0c\u53d1\u73b0\u65e0\u8bba\u6267\u884c\u591a\u5c11\u6b21List\u7684\u6570\u636e\u90fd\u80fd\u6309\u7167contact\u4e2dObservable\u987a\u5e8f\u53d1\u9001\uff0c\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u53ef\u4ee5\u5b9e\u73b0\u4e86\uff0c\u4e0d\u8fc7\u4f60\u4f1a\u53d1\u73b0\uff0c\u6548\u7387\u592a\u5dee\u4e86\uff0c\u8fd9\u662f\u540c\u6b65\u6267\u884c\u554a\uff0c\u53ea\u6709\u7b2c\u4e00\u4e2a\u8bf7\u6c42\u6210\u529f\uff0c\u624d\u4f1a\u53bb\u8bf7\u6c42\u7b2c\u4e8c\u4e2a\uff0c\u7136\u540e\u7b2c\u4e09\u4e2a\uff0c\u5047\u5982\u4e00\u6b21\u8bf7\u6c42\u9700\u8981\u4e00\u79d2\uff0c\u90a3\u4e09\u6b21\u8bf7\u6c42\u81f3\u5c11\u4e09\u79d2\u554a\uff0c\u4e0d\u80fd\u5fcd\u3002<\/p>\n

zip<\/strong><\/span><\/div>\n

\u9274\u4e8e\u4e0a\u9762\u4e24\u79cd\u65b9\u5f0f\u7684\u5229\u5f0a\uff0c\u5982\u679c\u6211\u4eec\u65e2\u60f3\u5982merge\u4e00\u6837\u5e76\u53d1\u6267\u884c\uff0c\u53c8\u60f3\u548ccontact\u4e00\u6837\u4fdd\u8bc1\u987a\u5e8f\uff0c\u662f\u4e0d\u662f\u6709\u70b9\u5f3a\u8feb\u75c7\u7684\u610f\u601d\uff0c\u5f53\u7136\u5f3a\u5927\u7684zip\u5c31\u80fd\u5b9e\u73b0\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u3002\u5982\u4e0b\u5b9e\u73b0\u3002<\/p>\n

    fun executeZip() {\r\n        Observable.zip(getGoodsObservable(1),\r\n                getGoodsObservable(2),\r\n                getGoodsObservable(3),\r\n                Function3<Goods, Goods, Goods, List<Goods>>\r\n                { goods0, goods1, goods2 ->\r\n                    val list = ArrayList<Goods>()\r\n                    list.add(goods0)\r\n                    list.add(goods1)\r\n                    list.add(goods2)\r\n                    list\r\n                }).subscribeOn(Schedulers.newThread())\r\n                .observeOn(AndroidSchedulers.mainThread())\r\n                .subscribe({\r\n                    Log.e(TAG, it.toString())\r\n                }, {\r\n                    Log.e(TAG, it.toString())\r\n                })\r\n    }<\/pre>\n

\u65e2\u7136\u5b9e\u73b0\u4e86\uff0c\u90a3\u6211\u4eec\u8fd0\u884c\u51e0\u6b21\uff0c\u53d1\u73b0\u5b8c\u7f8e\u7684\u5b9e\u73b0\u4e86\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\uff0c\u5373\u5e76\u53d1\u7684\u6267\u884c\u4e86\uff0c\u4e5f\u4fdd\u8bc1\u4e86\u6211\u4eec\u8bf7\u6c42\u6570\u636e\u7684\u987a\u5e8f\u6027\u3002<\/p>\n

\u5728\u56de\u8c03\u4e2d\u8fd0\u7528RxJava<\/strong><\/div>\n

\u5728\u4e0a\u9762\u6211\u4eec\u7684\u5355\u4e2a\u7f51\u7edc\u8bf7\u6c42\u662f\u4e00\u4e2a\u540c\u6b65\u7684\u8bf7\u6c42\uff0c\u5982\u679c\u6211\u4eec\u7684\u7f51\u7edc\u8bf7\u6c42\u5c01\u88c5\u4e86\uff0c\u5728\u7ebf\u7a0b\u4e2d\u8bf7\u6c42\uff0c\u8bf7\u6c42\u6210\u529f\u540e\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u56de\u8c03\uff0c\u90a3\u6211\u4eec\u53c8\u8be5\u5982\u4f55\u521b\u5efa\u5462\u4f7f\u7528\u5462\uff1f<\/p>\n

\u5148\u6765\u6a21\u62df\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u8bf7\u6c42\u7f51\u7edc\uff0c\u8bf7\u6c42\u6210\u529f\u56de\u8c03\u6570\u636e\u7ed9\u4e3b\u7ebf\u7a0b\u3002<\/p>\n

    fun getGoods(ctx:Context,id: Int,callbacks:(goods:Goods)->Unit): Unit {\r\n        ctx.doAsync {\r\n            Thread.sleep(Random().nextInt(1000).toLong())\r\n            var data = Goods(id, Random().nextInt(20), \"\u5730\u5740${id}\")\r\n            ctx.runOnUiThread {\r\n                callbacks(data)\r\n            }\r\n        }\r\n    }<\/pre>\n

getGoods\u4f20\u4e86\u4e09\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2aContext\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u662f\u5546\u54c1ID,\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c(goods:Goods)->Unit\u8868\u793a\u7b2c\u4e09\u4e2a\u53c2\u6570\u7684\u7c7b\u578b\u662f\u4e00\u4e2a\u53c2\u6570\u4e3aGoods\u7c7b\u578b\u5e76\u4e14\u8fd4\u56deUnit\u7684\u51fd\u6570\u3002\u4f7f\u7528doAsync \u6a21\u62df\u5f02\u6b65\u8bf7\u6c42\uff0c\u8bf7\u6c42\u6210\u529f\u540erunOnUiThread \u5207\u6362\u5230UI\u7ebf\u7a0b\u3002\u7136\u540ecallbacks\uff08data\uff09\u5c06\u6570\u636e\u56de\u8c03\u3002\u8fd9\u79cd\u4f7f\u7528\u65b9\u5f0f\u6bd4Java\u4e2d\u56de\u8c03\u4f18\u7f8e\u597d\u7528\u592a\u591a\u4e86\u3002<\/p>\n

\u63a5\u4e0b\u6765\u5c31\u5f00\u59cb\u5728\u56de\u8c03\u6210\u529f\u540e\u521b\u5efaObservable<\/p>\n

  fun getGoodsCallBack(id: Int): Observable<Goods> {\r\n        var subscrbe: ObservableEmitter<Goods>? = null\r\n        var o = Observable.create<Goods> {\r\n            subscrbe = it\r\n        }\r\n        \/\/Kotlin\u7279\u6027\r\n        getGoods(this@MainActivity, id) {\r\n            subscrbe?.onNext(it)\r\n        }\r\n        return o\r\n    }\r\n    fun executeZipCallBack() {\r\n        Observable.zip(getGoodsCallBack(1).subscribeOn(Schedulers.newThread()),\r\n                getGoodsCallBack(2).subscribeOn(Schedulers.newThread()),\r\n                getGoodsCallBack(3).subscribeOn(Schedulers.newThread()),\r\n                Function3<Goods, Goods, Goods, List<Goods>>\r\n                { goods0, goods1, goods2 ->\r\n                    val list = ArrayList<Goods>()\r\n                    list.add(goods0)\r\n                    list.add(goods1)\r\n                    list.add(goods2)\r\n                    list\r\n                }).subscribeOn(Schedulers.newThread())\r\n                .observeOn(AndroidSchedulers.mainThread())\r\n                .subscribe({\r\n                    Log.e(TAG, it.toString())\r\n                }, {\r\n                    Log.e(TAG, it.toString())\r\n                })\r\n    }<\/pre>\n

ok,\u5230\u8fd9\u91cc\u56de\u8c03\u60c5\u51b5\u4e0b\u521b\u5efa\u4f7f\u7528RxJava\u4e5f\u4ecb\u7ecd\u5b8c\u6bd5\uff0c\u5230\u6b64\u672c\u7bc7\u6587\u7ae0\u5c31\u7ed3\u675f\u4e86\uff0c\u6709\u95ee\u9898\u6b22\u8fce\u6307\u51fa\uff0c\u5185\u5bb9\u6742\u4e71\uff0c\u591a\u591a\u62c5\u5f85\uff0cHava a wonderful day.<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.codeceo.com\/article\/kotlin-and-rxjava-multiple-data-sources.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/kotlin-rxjava-multidata.html<\/a>\u7f16\u8f91\u5458\uff1a\u534e\u4e16\u53d1\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u672c\u6587\u4e3b\u8981\u8bb2\u89e3Kotlin\u8bed\u8a00\u5904\u7406RxJava\u591a\u6570\u636e\u6e90\u7684\u5b9e\u73b0\u3002\u9605\u8bfb\u672c\u6587\u6700\u597d\u6709Kotlin\u57fa\u7840\uff0c\u82e5\u6ca1\u6709\u57fa\u7840\uff0c […]<\/p>\n","protected":false},"author":63,"featured_media":85865,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-71163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=71163"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71163\/revisions"}],"predecessor-version":[{"id":85924,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71163\/revisions\/85924"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/85865"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=71163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=71163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=71163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}