Relationship queries in Realm Android - android

I am new to Realm. I have 3 classes that extends RealmObject whose objects i save to db.
Employee
FamilyMember
and Designation
FamilyMember and Designation is in relationship with Employee like this
public class Employee extends RealmObject{
private String name;
private int age;
private int id;
private Designation designation;
private RealmList <FamilyMember> familyMembers = new RealmList<>();
.
.
.getter setters
.
.
.
}
FamilyMember looks like this
public class FamilyMember extends RealmObject {
private String name;
private String relation;
.
.
.getter setters
.
.
.
}
Now i am able to query a Employee whose FamilyMember has "some" name but i am struggling to find a way to get the list of Employee who has more than four family members.
Can anyone help me? Thanks in advance.

It is unfortunately not easy in the current version of the query language. There is an issue tracking it here: https://github.com/realm/realm-java/issues/1598
Right now you would unfortunately have to find all families and manually filter for those with more than 4.

Related

How to implement Inner Join on Realm DB in android

I am searching everywhere but not found the exact way to use Inner Join on Realm DB in Android, check my code
public class UserName extends RealmObject {
private int UserID;
private String UserName;
// Getter Setters
}
public class UserDepartment extends RealmObject {
private int UserID;
private String UserDepartment;
// Getter Setters
}
In SQLite by using following query we easily get data
Select n.UserName, d.UserDepartment
FROM Name n INNER JOIN Department d
ON n.UserID=d.UserID
ORDER BY d.UserDepartment
But how to do same thing on Realm DB to get result...
Thanks in Advance!!
As you may know Realm is a non-relational database and concepts like join belongs for relational database. but if you need to have both UserName and UserDepartment in a single model class there are lots of implementations. but due to my experiences and according to realm constraints in using objects on different threads and updating objects snapshot with realm file, I suggest you to create a new entity, just like this:
class User extends RealmObject{
private int userId;
private UserName username;
private UserDepartment userDepartment;
}
whenever you insert a record into either UserName or UserDepartment you need to insert a record or update existing record in User.

Realm - Relationships 1 to n

I have two tables (Dog & Person) defined in Realm object and need use 1:n relation.
My Person class is:
public class Person extends RealmObject {
#PrimaryKey
private int id;
private int age;
private Dog dog;
/** Getters & setters **/
}
My Dog class is:
public class Dog extends RealmObject {
#PrimaryKey
public int id;
public String name;
/** Getters & setters **/
}
When the table "Dogs" contains data loading from internet. I need add dog id to my Person table as relationship, when save object Person. If save to "Person" table dogId, than i dont join data. I need select data from Person include dog name.
Is possible in realm as relationship? Or I use another realm query, where i will search by dog where id?
Thank you for response.

Android Realm.io - Removing sub-objects

Let's say I have a structure of realm-objects that looks like this -
public class Person extends RealmObject {
#PrimaryKey
private int id;
private String name;
private List<Pet> pets
// Setters, getters...
}
public class Pet extends RealmObject {
private String name;
private MedicalRecord record;
// Setters, getters...
}
public class MedicalRecord extends RealmObject {
private String something;
private String somethingElse;
// Setters, getters...
}
Now I received a new Person object with an existing id (primary-key) and I want to update this person.
So I do something like this -
realm.beginTransaction();
realm.copyToRealmOrUpdate(person);
realm.commitTransaction();
The trouble is that this person's pet list (and the pets' medical records), are still out there in the db. not linked anymore to this person, but still there.
I tried to do this -
Person existingPerson = realm.where(Person .class).equalTo("id", ID).findFirst();
existingPerson.getPets().clear();
But no success there. How can I remove subobjects of realmObjects?
Also, is there a way to define a policy for a realm-object so that it will remove itself once there is no reference to it (it's not linked to any parent-object)?
Now you can, and method was renamed from last commit to realmList.deleteAllFromRealm()

Searching an Object by it's RealmList

I have a Person object and it has a RealmList of phone numbers.
How can I search a person by a phone number? I'm using Realm for my Android project.
Here are my models:
class Person extends RealmObject{
private RealmList<Contact> emails;
private RealmList<Contact> phones;
}
class Contact extends RealmObject{
private String type;
private String value;
}
I need to search a person by email and phone.
Thanks for any suggestion!
You can chain properties with '.'
So in your case you could do:
realm.where(Person.class).equalTo("phones.value", whateverYouWant).findAll()
Obviously you would replace 'whateverYouWant' with the value you want to search with.
Hope that helps.

Realm query two object in Android

there is a problem when I use the Realm in Android.
I wrote two RealmObject.
public class Feed extends RealmObject {
#PrimaryKey
private long id;
private String content;
private long uid;
...
}
public class User extends RealmObject {
#PrimaryKey
private long uid;
private String name;
...
}
I want to search the result with:
[feed_id, feed_content, user_id, user_name ...]
should I need add a new Object ( FeedUser extends RealmObject) with these fields? Is this waste the memory?
Also I want to listen the change about user Object, if I add the FeedUser, when User changed. How to update FeedUser synchronous ?
thx :)
Take a look at how Relationships work in Realm.
If a user can have multiple Feed objects then you can have List<Feed> in your User object instead of defining user id yourself in your Feed. It will be something like this. You can read documentation more to see how you will get User with its feed in one query.
public class Feed extends RealmObject {
#PrimaryKey
private long id;
private String content;
...
}
public class User extends RealmObject {
#PrimaryKey
private long uid;
private String name;
private RealmList<Feed> feeds;
...
}

Categories

Resources