可观察的android理解(Observable android understanding)

编程入门 行业动态 更新时间:2024-10-10 13:15:00
可观察的android理解(Observable android understanding)

我试图了解观察者模式在Android中是如何工作的。

我创建了这个方法来加载对象的样本列表,将每个项目推送到订阅者并将其加载到recyclerview中。

我不明白为什么如果我加载10个项目一切正常,但如果我加载100/1000或一般更多的项目,recyclelerView是空的onNext,onComplete不会被触发。

private Observable<AppInfo> getAppList() { return Observable.create(new Observable.OnSubscribe<AppInfo>() { @Override public void call(Subscriber<? super AppInfo> subscriber) { for (int i = 0; i<10; i++){ AppInfo appInfo = new AppInfo( "Test item "+i, ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher), i ); subscriber.onNext(appInfo); } if (!subscriber.isUnsubscribed()) { subscriber.onCompleted(); } } }); }

这就是我使用Observable的方式:

Observable<AppInfo> appInfoObserver = getAppList(); appInfoObserver .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<AppInfo>() { @Override public void onCompleted() { Toast.makeText(getApplicationContext(), "App List Load Completed!", Toast.LENGTH_LONG).show(); } @Override public void onError(Throwable e) {} @Override public void onNext(AppInfo appInfo) { if(mAppInfoList != null){ mAppInfoList.add(appInfo); adapter.notifyItemInserted(appInfo.getAppPosition()); } } });

感谢您的帮助和建议。

I'm trying to understand how the observer pattern works in Android.

I've created this method to load a sample list of object, pushing each items to the subscriber and loading it to into the recyclerview.

I don't understand why if i load 10 items everything is working fine, but if i load 100/1000 or in general more items, the recyclerView is empty and onNext, onComplete are not fired.

private Observable<AppInfo> getAppList() { return Observable.create(new Observable.OnSubscribe<AppInfo>() { @Override public void call(Subscriber<? super AppInfo> subscriber) { for (int i = 0; i<10; i++){ AppInfo appInfo = new AppInfo( "Test item "+i, ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher), i ); subscriber.onNext(appInfo); } if (!subscriber.isUnsubscribed()) { subscriber.onCompleted(); } } }); }

And this is how i use the Observable:

Observable<AppInfo> appInfoObserver = getAppList(); appInfoObserver .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<AppInfo>() { @Override public void onCompleted() { Toast.makeText(getApplicationContext(), "App List Load Completed!", Toast.LENGTH_LONG).show(); } @Override public void onError(Throwable e) {} @Override public void onNext(AppInfo appInfo) { if(mAppInfoList != null){ mAppInfoList.add(appInfo); adapter.notifyItemInserted(appInfo.getAppPosition()); } } });

Thanks for the help and advices.

最满意答案

你没有记录错误,所以如果出现任何问题,你就不会知道(在这种情况下,你可能会通过发送一个比它要求更多的MissingBackpressureException来强迫来自observeOn运算符的observeOn )。 要明确,在订户中:

public void onError(Throwable e) { // log or display error here!! }

如果可以帮助它,请不要使用Observable.create ,因为您需要尊重背压或将其与.onBackpressureBuffer结合使用。

例外情况是Observable.create(new SyncOnSubscribe<T>(...))是一种创建Observable的好方法,如果您可以将源代码视为迭代器/枚举。

要避免在示例中使用Observable.create ,您可以这样做:

Observable .range(0, 10) .map(i -> new AppInfo(...))

或没有lambda:

Observable .range(0, 10) .map(new Func1<Integer, AppInfo>() { @Override public AppInfo call(Integer n) { return new AppInfo(...); } });

You're not logging errors so if anything goes wrong you won't know (in this case you are probably forcing a MissingBackpressureException from the observeOn operator by sending it more than it requested). To be clear, in the subscriber:

public void onError(Throwable e) { // log or display error here!! }

Don't use Observable.create at all if you can help it because you need to honour backpressure or combine it with .onBackpressureBuffer.

The exception is that Observable.create(new SyncOnSubscribe<T>(...)) is a good way to create an Observable if you can imagine your source as an iterator/enumeration.

To avoid using Observable.create in your example you could do this:

Observable .range(0, 10) .map(i -> new AppInfo(...))

or without lambda:

Observable .range(0, 10) .map(new Func1<Integer, AppInfo>() { @Override public AppInfo call(Integer n) { return new AppInfo(...); } });

更多推荐

本文发布于:2023-04-29 01:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:android   Observable   understanding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!