Reputation: 2485
I have following Model
public class VideosOwn {
public long _id;
public String _title;
public String _width;
public long _height;
public String _orientation;
public long _size;
public VideosOwn(long _id, String _title, String _width, long _height, String _orientation, long _size) {
this._id = _id;
this._title = _title;
this._width = _width;
this._height = _height;
this._orientation = _orientation;
this._size = _size;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
and following method to get the data as an array using ContentResolover
public ArrayList<VideosOwn> GetVideoList(Context mContext) {
ArrayList<VideosOwn> videos = new ArrayList<>();
Cursor cursor = mContext.getContentResolver().query(VideosUri,
VideosProjection, null, null, MediaStore.Video.Media.DATE_ADDED + " DESC");
if (cursor != null && cursor.moveToFirst()) {
do {
VideosOwn videoval = new VideosOwn(
cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE)),
cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.WIDTH)),
cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.HEIGHT)),
cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.ORIENTATION)),
cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE))
);
videos.add(videoval);
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
return videos;
}
I am new to Rxjava/RxAndroid, I want emit The data as an ArrayList
How can that be done , Should we use Disposalbe
, Observable.fromCallable
, what would be the Best solution.
Upvotes: 0
Views: 192
Reputation: 9929
As you always want a single result and it will be a cold observable, you can use a Single
, something like these examples :
Emitter :
public Single<List<VideosOwn>> getVideos(Context context) {
return Single.create(emitter -> {
try {
final List<VideosOwn> result = GetVideoList(context);
emitter.onSuccess(result);
} catch (Exception e) {
emitter.onError(e);
}
});
}
Callable :
public Single<List<VideosOwn>> getVideos(Context context) {
return Single.fromCallable(() -> GetVideoList(context));
}
Usage :
// Retain Disposable and call d.dispose() if you no longer want to subscribe to the result
final Disposable d = getVideos(context)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(r -> {
// do something with list
}, e -> {
// log error or some other handle
});
Upvotes: 1