代码之家  ›  专栏  ›  技术社区  ›  Khemraj Sharma

从单个<list>可观察对象获取列表

  •  0
  • Khemraj Sharma  · 技术社区  · 7 年前

    我有一个方法返回 Single<List<HistoryItem>>

    private fun getListOfDownload(): ArrayList<HistoryItem> {
        val downloads = manager.lastHundredVisitedHistoryItems()
        downloads.subscribe { t ->
            return t; // not allowed
        }
    }
    

    我可以用 subscribe

    我试过了 map 也。

    downloads.map { t: List<HistoryItem> -> return t } // again `return t` is not allowed.
    

    我可以按照下面的方法来做

    private fun getListOfDownload(): ArrayList<HistoryItem> {
            var list = ArrayList<HistoryItem>()
            val downloads = manager.lastHundredVisitedHistoryItems()
            downloads.subscribe { t ->
                list = t as ArrayList<HistoryItem>
            }
            return list
        }
    

    • 如果这样做是对的,它不会阻塞主线程吗?
    • 有没有办法在不声明另一个列表的情况下返回列表

    我是反应式编程新手,请解释一下。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Moinkhan    7 年前

    在你的代码中 getListOfDownload ArrayList<HistoryItem> ,而 lastHundredVisitedHistoryItems 属于 manager 返回 Single<ArrayList<HistoryItem>> 阵列列表<历史项目> 上百个预测历史项目 需要一些时间。

    阵列列表<历史项目> 物品。

    但如果你不在乎观察结果你可以做这样的事,

    private fun getListOfDownload(): ArrayList<HistoryItem> {
        val downloads = manager.lastHundredVisitedHistoryItems()
        return downloads.blockingGet()
    }
    

    但是你失去了异步调用 上百个预测历史项目 当我们申请时 blockingGet() ,但由于您没有在单独的线程上观察到它,所以我假设您希望在主线程上调用它。

    lastHundredVisitedHistoryItems() 然后可以将方法作为参数传递,该参数将充当 阵列列表<历史项目> .

    private fun getListOfDownload( listener : (ArrayList<LoginViewModel.HistoryItem>) -> Unit) {
            val downloads = manager.lastHundredVisitedHistoryItems()
            downloads.subscribe { t -> listener.invoke(t) }
    }
    
    // when you call above method look like below
    // here variable `it` is nothing but a object of ArrayList<LoginViewModel.HistoryItem>
    getListOfDownload { adapter.updateDataSet(it) }
    
        2
  •  1
  •   Mark Gilchrist    7 年前

    我认为订阅的lambda是 crossinline 意思是不允许本地返回

    subscribe{}

    例如

    val compositeDisposable = CompositeDisposable()
    
    private fun downloadList() {
        manager.lastHundredVisitedHistoryItems()
            downloads.subscribe { downloadedList ->
                this.list = downloadedList
                // or
                adapter.updateDataSet(downloadedList)
            }.addTo(compositeDisposeable)
        }
    }
    

    您还需要将一次性添加到 CompositeDisposable 防止记忆韭菜的可能性

        3
  •  0
  •   Rishav Singla    7 年前

    您可以使用订阅获取列表,如下所示:

     @NonNull
    public Single<String> getHistoryPage() {
        return Single.create(new SingleAction<String>() {
            @Override
            public void onSubscribe(@NonNull final SingleSubscriber<String> subscriber) {
    
    
                mHistoryModel.lastHundredVisitedHistoryItems()
                        .subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
                            @Override
                            public void onItem(@Nullable List<HistoryItem> item) {
                          final StringBuilder historyBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);      
    
                                Iterator<HistoryItem> it = item.iterator();
                                HistoryItem helper;
                                while (it.hasNext()) {
                                    helper = it.next();
                                    historyBuilder.append(PART1);
                                    historyBuilder.append(helper.getUrl());
                                    historyBuilder.append(PART2);
                                    historyBuilder.append(helper.getTitle());
                                    historyBuilder.append(PART3);
                                    String url = helper.getUrl();
                                    try {
                                        URL url1 = new URL(url);
                                        authority = url1.getAuthority();
                                    } catch (MalformedURLException e) {
                                        e.printStackTrace();
                                    }
                                    historyBuilder.append(authority);   //No need of path showing of History file.
                                    historyBuilder.append(PART4);
                                }
    
                                historyBuilder.append(END);
                                File historyWebPage = getHistoryPageFile(mApp);
                                FileWriter historyWriter = null;
                                try {
                                    //noinspection IOResourceOpenedButNotSafelyClosed
                                    historyWriter = new FileWriter(historyWebPage, false);
                                    historyWriter.write(historyBuilder.toString());
                                } catch (IOException e) {
                                    Log.e(TAG, "Unable to write history page to disk", e);
                                } finally {
                                    Utils.close(historyWriter);
                                }
    
                                subscriber.onItem(Constants.FILE + historyWebPage);
                                subscriber.onComplete();
                            }
                        });
            }
        });
    }