-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ChangeListener called after removeChangeListener #2401
Comments
mHardware.getValues().where().findAll().removeChangeListener(mListener); this line equals to RealmResults newResults = mHardware.getValues().where().findAll();
newResults.removeChangbeListener(mListener); It creates a new To fix this: private RealmResults results;
@Override
public void onResume() {
redraw();
if(mHardware != null) {
// You might also want to check if the results is null here, no need to rebuild a Results if it exists.
results = mHardware.getValues().where().findAll();
results.addChangeListener(mListener);
}
super.onResume();
}
@Override
public void onPause() {
if(mHardware != null){
results.removeChangeListener(mListener);
}
super.onPause();
} |
what do i need to do in the adapter? package com.hkm.hbstore.adapters.bookmark;
import android.support.v7.widget.RecyclerView;
import com.hypebeast.sdk.api.realm.hbx.rProduct;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.quickAdapter.easyRegularAdapter;
import java.util.ArrayList;
import java.util.List;
import io.realm.RealmChangeListener;
import io.realm.RealmObject;
import io.realm.RealmResults;
/**
* Created by hesk on 9/3/16.
*/
public abstract class realmUltimateAdapter<T extends RealmObject, BINDHOLDER extends UltimateRecyclerviewViewHolder> extends easyRegularAdapter<T, BINDHOLDER> {
protected RealmResults<T> source;
private final RealmChangeListener listener;
public realmUltimateAdapter(boolean automaticUpdate, RealmResults<T> source) {
super(source);
this.source = source;
listener = (!automaticUpdate) ? null : new RealmChangeListener() {
@Override
public void onChange() {
notifyDataSetChanged();
}
};
if (listener != null && source != null) {
// source.realm.handlerController.addChangeListenerAsWeakReference(listener);
source.addChangeListener(listener);
}
}
@Override
public int getAdapterItemCount() {
return source.size();
}
protected T getItem(final int pos) {
synchronized (mLock) {
return source.get(pos);
}
}
/**
* Determine if the object provide is in this adapter
*
* @return true if the object is in this adapter
*/
public boolean hasItem(T object) {
synchronized (mLock) {
return source.contains(object);
}
}
/**
* Returns whether this {@code List} contains no elements.
*
* @return {@code true} if this {@code List} has no elements, {@code false}
* otherwise.
* @see #source
*/
public boolean isEmpty() {
return source.size() == 0;
}
/**
* @return a copy of the {@code List} of elements.
*/
public RealmResults<T> getObjects() {
synchronized (mLock) {
return source;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// if (position >= getAdapterItemCount()) return;
if (getItemViewType(position) == VIEW_TYPES.ADVIEW) {
onBindAdViewHolder(holder, position);
} else if (getItemViewType(position) == VIEW_TYPES.CUSTOMVIEW) {
onBindCustomViewHolder(holder, position);
} else if (getItemViewType(position) == VIEW_TYPES.HEADER) {
onBindHeaderViewHolder(holder, position);
} else if (getItemViewType(position) == VIEW_TYPES.FOOTER) {
onBindFooterViewHolder(holder, position);
} else if (getItemViewType(position) == VIEW_TYPES.NORMAL) {
// if (position >= getAdapterItemCount()) return;
T object;
synchronized (mLock) {
object = source.get(getItemDataPosFromInternalPos(position));
}
withBindHolder((BINDHOLDER) holder, object, position);
}
}
public final void removeInternalR(int position) {
if (hasHeaderView() && position == 0) return;
if (enableLoadMore() && position == getItemCount() - 1) return;
if (source.size() > 0) {
synchronized (mLock) {
source.removeChangeListener(listener);
source.remove(hasHeaderView() ? position - 1 : position);
}
notifyItemRemoved(position);
}
}
} |
finally this is my solution public final void removeInternalRealmSupport(int position) {
if (hasHeaderView() && position == 0) return;
if (enableLoadMore() && position == getItemCount() - 1) return;
if (source.size() > 0) {
synchronized (mLock) {
Realm realm = Realm.getInstance(RealmUtil.realmCfg(ctm));
realm.beginTransaction();
source.remove(hasHeaderView() ? position - 1 : position);
realm.commitTransaction();
}
notifyItemRemoved(position);
}
} add this above code in the adapter to get all the remove issue resolve. |
|
Please just ignored what i said above. |
I have tried |
@beeender i think it is still not the perfect solution as i have recall the realm from the initiation. Are there any options that i can call directly from the |
@jjhesk Why not just add the Realm instance as an extra parameter in the constructor? |
Goal (what do you want to achieve?)
Notify on change
Expected Results
Not being notified when removing the listener
Actual Results (e.g. full stack trace with exception)
Notification fires after the listener has been removed
Steps & Code to Reproduce (Or describe your current debugging efforts)
The activity containing the views depends on multiple records. I want these views to be updated in real time, so i've put a listener to the child list. After removing the listener (and after the activity has been destroyed), the listener is still being called.
Version of Realm and tooling
Realm version(s): 0.87.5
Android Studio version: 1.5.1
Android version(s) on device/simulator: 21
The text was updated successfully, but these errors were encountered: