Sutirth
Sutirth

Reputation: 1267

Realm Android queries return wrong result

I have table which has a structure as follows:

public class GroupDailyDB extends RealmObject{
    @PrimaryKey
    private int groupId;
    private String groupName;
    private String groupDescription;
    private int groupStatus;
    private boolean hasImage;
    private int sweatPoints;
    private long steps;
    private int rank;
    private float percentile;
    private int noOfUsers;
    private boolean isCurrentUserPartOfGroup;
    private boolean isCurrentUserOwner;
} 

My data is getting saved successfully enter image description here My json when refreshes does not send group Id 3 and I need to change its status.

The code for checking is as follows:

for(int i = 0 ; i < groupModel.getData().size() ; i++){
    GroupDailyDB groupDailyDB = 
           realm.where(GroupDailyDB.class)
           .notEqualTo("groupId",groupModel.getData().get(i).getGroupId())
           .findFirst();
    Log.i(TAG,"Group id delete daily:  "+groupDailyDB.getGroupId());
}

But my log prints id 4,5 respectively it never returns me id 3

Upvotes: 1

Views: 653

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191983

So, think about this... You have a list of 3 objects, each with a unique ID value. You loop over some other list of ID values, querying the 3 objects where its ID is not equal to the current ID of the loop.

That query is going to return 2 elements for the case of a matching ID (you filtered it out) and 3 elements for a non-matching ID. You only get the first element of these queries, and you are confused about missing data?

In the case of the unique ID values, you should use equalTo to find the one element that matches. Also might want to add some exception check for when no ID value is equal (when findFirst returns no object)

Upvotes: 1

EpicPandaForce
EpicPandaForce

Reputation: 81588

Shouldn't your code be something like

RealmQuery<GroupDailyDb> query = realm.where(GroupDailyDB.class);
for(int i = 0 ; i < groupModel.getData().size() ; i++){
    query = query.notEqualTo("groupId",groupModel.getData().get(i).getGroupId());
}
GroupDailyDB groupDailyDB = query.findFirst();
Log.i(TAG,"Group id delete daily:  "+groupDailyDB.getGroupId());

?

Upvotes: 0

Related Questions