从 Single<List> observable 获取列表
Get list from Single<List> observable
我有一个方法 returns Single<List<HistoryItem>>
。现在我想从中获取列表。
private fun getListOfDownload(): ArrayList<HistoryItem> {
val downloads = manager.lastHundredVisitedHistoryItems()
downloads.subscribe { t ->
return t; // not allowed
}
}
我可以使用 subscribe
但这不允许我从中 return 列表。
我也试过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
}
请告诉
- 如果这种方式是对的,不会阻塞主线程吗?
- 有没有办法在不声明另一个列表的情况下 return
名单?
我是 Reactive 编程的新手,所以请解释一下。
我认为订阅 lambda 是 crossinline
意味着它不允许本地 returns
您通常在这里做的是在 subscribe{}
中将列表用于其目的(更新 recyclerView)
例如
val compositeDisposable = CompositeDisposable()
private fun downloadList() {
manager.lastHundredVisitedHistoryItems()
downloads.subscribe { downloadedList ->
this.list = downloadedList
// or
adapter.updateDataSet(downloadedList)
}.addTo(compositeDisposeable)
}
}
你还需要把你的disposable加到一个CompositeDisposable
中,防止出现内存韭菜的可能
在您的代码中 getListOfDownload
是 returning ArrayList<HistoryItem>
,而 lastHundredVisitedHistoryItems
of manager
returning Single<ArrayList<HistoryItem>>
这意味着你想观察 ArrayList<HistoryItem>
,因为 lastHundredVisitedHistoryItems
需要一些时间。
这就是为什么你不能 return 订阅块中的项目,因为该块是 lamda,它将观察 ArrayList<HistoryItem>
项目。
但是如果你不关心观察你可以做这样的事情,
private fun getListOfDownload(): ArrayList<HistoryItem> {
val downloads = manager.lastHundredVisitedHistoryItems()
return downloads.blockingGet()
}
但是你失去了 lastHundredVisitedHistoryItems
的异步调用,因为我们正在应用 blockingGet()
,但是因为你没有在单独的线程上观察它,我假设您想在主线程上调用它。
但是如果你想观察 lastHundredVisitedHistoryItems()
那么你可以将方法作为参数传递,它将作为 ArrayList<HistoryItem>
.
的消费者
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) }
您可以使用订阅获取列表如下:
@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();
}
});
}
});
}
通过使用上面的代码,您可以在 html 页面中获取历史列表。为此,您必须使用 StringBuilder,然后才能获得完整的历史记录页面
我有一个方法 returns Single<List<HistoryItem>>
。现在我想从中获取列表。
private fun getListOfDownload(): ArrayList<HistoryItem> {
val downloads = manager.lastHundredVisitedHistoryItems()
downloads.subscribe { t ->
return t; // not allowed
}
}
我可以使用 subscribe
但这不允许我从中 return 列表。
我也试过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
}
请告诉
- 如果这种方式是对的,不会阻塞主线程吗?
- 有没有办法在不声明另一个列表的情况下 return 名单?
我是 Reactive 编程的新手,所以请解释一下。
我认为订阅 lambda 是 crossinline
意味着它不允许本地 returns
您通常在这里做的是在 subscribe{}
例如
val compositeDisposable = CompositeDisposable()
private fun downloadList() {
manager.lastHundredVisitedHistoryItems()
downloads.subscribe { downloadedList ->
this.list = downloadedList
// or
adapter.updateDataSet(downloadedList)
}.addTo(compositeDisposeable)
}
}
你还需要把你的disposable加到一个CompositeDisposable
中,防止出现内存韭菜的可能
在您的代码中 getListOfDownload
是 returning ArrayList<HistoryItem>
,而 lastHundredVisitedHistoryItems
of manager
returning Single<ArrayList<HistoryItem>>
这意味着你想观察 ArrayList<HistoryItem>
,因为 lastHundredVisitedHistoryItems
需要一些时间。
这就是为什么你不能 return 订阅块中的项目,因为该块是 lamda,它将观察 ArrayList<HistoryItem>
项目。
但是如果你不关心观察你可以做这样的事情,
private fun getListOfDownload(): ArrayList<HistoryItem> {
val downloads = manager.lastHundredVisitedHistoryItems()
return downloads.blockingGet()
}
但是你失去了 lastHundredVisitedHistoryItems
的异步调用,因为我们正在应用 blockingGet()
,但是因为你没有在单独的线程上观察它,我假设您想在主线程上调用它。
但是如果你想观察 lastHundredVisitedHistoryItems()
那么你可以将方法作为参数传递,它将作为 ArrayList<HistoryItem>
.
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) }
您可以使用订阅获取列表如下:
@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();
}
});
}
});
}
通过使用上面的代码,您可以在 html 页面中获取历史列表。为此,您必须使用 StringBuilder,然后才能获得完整的历史记录页面