未调用RealmChangeListener

时间:2016-06-23 22:01:57

标签: android realm

我正在尝试将列表视图与Realm表链接,因此当项目添加到表中时,应更新列表视图。问题是添加项目时不会调用RealmChangeListener。

Realm real = RealmController.getInstance(getContext()); // Singleton
RealmResults<Friend> friends = real.where(Friend.class).findAllAsync();
friends.addChangeListener(callback);

RealmChangeListener<RealmResults<Friend>> callback=new RealmChangeListener<RealmResults<Friend>>() {
    @Override
    public void onChange(RealmResults<Friend> element) {
        // This is only called on async completed but not later
    }
};

保存过程在retrotif响应中完成:

    new MyService(context).userService().getFriends().enqueue(new Callback<FriendResponse>() {
        @Override
        public void onResponse(Call<FriendResponse> call, Response<FriendResponse> response) {
            Realm realm = RealmController.getInstance(context); // singleton
            realm.beginTransaction();
            realm.copyToRealmOrUpdate(response.body().friends);
            realm.commitTransaction();
        }

        @Override
        public void onFailure(Call<AllInfoResponse> call, Throwable t) {

        }
    });

你知道发生了什么吗?有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我遇到了一些与您的确切情况有关的问题(通过RealmChangeListener刷新了改造调用和UI),我即将发布一个新问题,但我只是想出来了。

在我的情况下,我在我的RealmChangeListener中实施了ArrayAdapter,我发现它更干净了:

public class ListAdapter extends ArrayAdapter<Item> implements
        RealmChangeListener<RealmResults<Item>> {

    private final List<Item> items;

    public ListAdapter(Context context, RealmResults<Item> items) {
        super(context, R.layout.listitem_item);
        this.items = items;
        items.addChangeListener(this);
    }

    @Override
    public void onChange(RealmResults<Restaurant> element) {
        notifyDataSetChanged();
    }

    ...
}

另一种情况中,我正在初始化视图并在活动的Retrofit中调用我的onCreate服务,并且在收到响应之前,某些方式没有附加监听器。所以,我做的另一件事是在Retrofit方法中调用onPostCreate,确保监听器准备就绪。

public class ItemListActivity BaseActivity implements
        RealmChangeListener<RealmResults<Item>>,
        Callback<GetItemsResponse> {

    private long id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);

        RealmResults<Item> items = getRealm()
                .where(Item.class)
                .findAll();

        items.addChangeListener(this);

        setupUI(items);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        MyRetrofitClient
                .getInstance(this)
                .getItems()
                .enqueue(this);
    }

    @Override
    public void onResponse(Call<GetItemsResponse> call, final Response<GetItemsResponse> response) {
        getRealm().executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                List<Item> newItems = response.body().getNewItems();
                realm.copyToRealmOrUpdate(newItems);
            }
        });
    }

    @Override
    public void onChange(RealmResults<Item> items) {
        updateUI();
    }

    ...
}

希望这有任何帮助!