Reputation: 418
I just learned about realm and something strange occurred to me. I have 3 activities lets call it A, B, and C.
In activity A (Home Screen) I call a service to download data from internet and stored it in realm.
In activity B (View Screen) I display all of the data using RealmResult and recyclerview
List<Data> dataList = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
rvAdapter = new RvAdapter(dataList);
recyclerView.setAdapter(rvAdapter);
RealmResults<Data> manyData = realm.where(Data.class)
.equalTo("date", currentDate)
.findAll();
for (int i = 0; i < manyData.size(); i++) {
dataList.add(new Data(manyData.get(i).getName(),manyData.get(i).getAge()));
}
rvAdapter.notifyDataSetChanged();
In activity C (Insert/Update Screen) I try to insert or update the data
Well at first from Activity A and B there is nothing wrong. The recyclerview display the data as I expected. Then something strange happened after I update an object in Activity C.
using
Data data = new Data();
data.setId(id);
data.setName(name);
data.setAge(age);
data.setDate(today);
realm.beginTransaction();
realm.copyToRealmOrUpdate(data);
realm.commitTransaction();
realm.close();
finish();
after finish(); activity C is cleared from the stack and activity B displayed. But the data which I have updated were removed from the recyclerview which is weird. When I print the manyData.size() in Activity B it shown that the size is smaller by 1 element. Can someone give me some guidance?
Upvotes: 1
Views: 147
Reputation: 58
I assume that the problem might be the following:
manyData
in B
you filter by date
.Data
-object in C
, the date
field is
not set. This results in the date
field being updated to null
.B
and filter for currentDate
, the updated
Data
-object will not be found since its date
field is null
.As far as I know you cannot not just update certain fields and leave the rest unchanged. Every field will be updated, including those which are null
(since that could be the desired value which you want to update).
I think the only way to achieve this is by updating from Json, where you can specifically leave certain fields out instead of setting them to null
.
Upvotes: 1