-
Notifications
You must be signed in to change notification settings - Fork 422
mDataManager.getContentQuery(catId).subscribe called multiples times #171
Comments
Can you provide an executable test case that demonstrates the problem? |
Hi, I have a RecyclerView, When user reach to end of list, I call an API and fetch data from Server and add that data in Local database as `BriteDatabase.Transaction transaction = db.newTransaction();
and after inset I fetch data from Local database as `mDataManager.getContentQuery(catId)
`public Observable<SqlBrite.Query> getContentQuery(int catId) {
Now problem is that |
Like If I print
where I run only last one query and previous queries was run in past. Whenever I run any query It run all past query also. |
Without a test case or a sample it's impossible to know what's going on. |
Hi I have added a sample project https://github.com/joginder89/SqlBriteSample . |
@joginder89 It looks like you have no unsubscribe logic(showContent method). So every time when you call showContent(int catId), it adds one more Observer<SqlBrite.Query> to already existing. You need to add some subscription management logic to release those subscription that are no longer needed, something like this: private Subscription mContentSubscription;
public void showContent(int catId) {
Log.e(TAG, "showContent=>Start");
if (mContentSubscription != null && !mContentSubscription.isUnsubscribed()) {
mContentSubscription.unsubscribe();
}
mContentSubscription = MyApplication.get().getDataManager().getContentQuery(catId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<SqlBrite.Query>() {
@Override
public void call(SqlBrite.Query query) {
Log.e(TAG, "showContent=>query-" + query);
Cursor cursor = query.run();
Log.e(TAG, "showContent=>cursor.getCount()-" + cursor.getCount());
List<ContentModel> result = new ArrayList<>();
while (cursor.moveToNext()) {
ContentModel content = Db.ContentTable.parseCursor(cursor);
result.add(content);
}
cursor.close();
updateAdapter(result);
}
});
} |
Basically what @rostyslav-y said, you need to track subscriptions to prevent old queries from triggering over and over. This is a general RxJava idiom, and not specific to this library. |
I have created a getContentQuery(int catId) method which return Observable<SqlBrite.Query> . Problem is that when I called mDataManager.getContentQuery(catId).subscribe() then call(SqlBrite.Query query) of Action1 inside .subscribe() executes multiple times. I used .doOnNext(new Action1<SqlBrite.Query>() this also called multiples times.
The text was updated successfully, but these errors were encountered: