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.
Related
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.
Following the official documents guide: Realm Relationships
I tried to write some code of Many-to-One relationship, and I found some things that are not consistent.
Two POJO Contace.java and Email.java are defined as follows:
public class Email extends RealmObject {
private String address;
private boolean active;
// ... setters and getters left out
}
public class Contact extends RealmObject {
private String name;
private Email email;
// ... setters and getters left out
}
Situation 1: Create a normal Email object and assign to different Contact contactA and contactB.
Email email = new Email();
email.setAddress("realm_test#gmail.com");
email.setActive(true);
Contact contactA = new Contact();
contactA.setName("Bear");
contactA.setEmail(email);
Contact contactB = new Contact();
contactB.setName("Monkey");
contactB.setEmail(email);
realm.beginTransaction();
realm.copyToRealm(contactA);
realm.copyToRealm(contactB);
realm.commitTransaction();
After I call realm.copyToRealm(), two Email objects will be created here. So, when I query Contact from Realm, one Email object will become two different object? I think this is not a Many-to-One relationship anymore, it just become One-to-One relationship.
Situation 2: Call realm.createObject() to create a proxy Email object and two proxy Contact object, and assign email to contactA and contactB.
realm.beginTransaction();
Email email = realm.createObject(Email.class);
email.setAddress("realm_test#gmail.com");
email.setActive(true);
Contact contactA = realm.createObject(Contact.class);
contactA.setName("Bear");
contactA.setEmail(email);
Contact contactB = realm.createObject(Contact.class);
contactB.setName("Monkey");
contactB.setEmail(email);
realm.commitTransaction();
Here we can see just one Email object in the table, and that's what I expected, just as described in the document above.
So, why is there no consistency in situation1 and situation2? Is it a bug in situation1? Or am I missing something?
Realm version(s):0.88.3
Android Studio version:2.0
Looking forward to your reply! Thanks!
Thanks for beeender's answer here: [issue-2730]
This is the expected behaviour.
In the situation1, the email you set to contactA and contactB is a standalone object which is not managed by Realm. So when you copy it to Realm, Realm has no way to know you mean they are the same object. To solve this, you can add a #PrimaryKey to Email and then use copyToRealmOrUpdate. Realm will try to detect if you mean the same email object for both contactA and contactB based on the primary key.
In the situation2, since the email object is managed by Realm, when you call setters, Realm knows that email is actually the same one.
Finally I set a #PrimaryKey both in Contact and Email, as following:
public class Email extends RealmObject {
#PrimaryKey
private String address;
private boolean active;
// ... setters and getters left out
}
public class Contact extends RealmObject {
#PrimaryKey
private String name;
private Email email;
// ... setters and getters left out
}
Then change copyToRealm() to copyToRealmOrUpdate() on contactA and contactB.
realm.beginTransaction();
realm.copyToRealmOrUpdate(contactA);
realm.copyToRealmOrUpdate(contactB);
realm.commitTransaction();
And the relationships betweens Contact and Email was correctly in situation 1.
In the first case two email is created because you are doing copyToRealm two times on different instance.
From the docs Each contact (instance of Contact) have either 0 or 1 email (instance of Email)
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.
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()
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;
...
}