Realm query two object in Android - 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;
...
}

Related

Android Realm one to many relationship, query

I have two classes:
public class Employee extends RealmObject {
#PrimaryKey
long id;
String name
Department department;
//getters setters etc.
}
public class Department extends RealmObject {
#PrimaryKey
int id;
String name;
//getters setters etc.
}
It is one to many relationship. One department can have multiple employees. I've tried to make some queries.
realm.where(Employee.class).equalTo("department.name", "deptName").findAll()
realm.where(Department.class).equalTo("employee.name", "empName").findAll()
The first one is working, the second one has an error:
Invalid query: employee does not refer to a class.
How to make the second one work? Do I must make a RealmList of Employees in Department class?
I wanted in this query fetch all Departments which have an Employee with specified name.
Realm 3.5.0+:
public class Employee extends RealmObject {
#PrimaryKey
long id;
String name
Department department;
//getters setters etc.
}
public class Department extends RealmObject {
#PrimaryKey
int id;
String name;
#LinkingObjects("department")
private final RealmResults<Employee> employees = null;
//getters setters etc.
}
realm.where(Employee.class).equalTo("department.name", "deptName").findAll()
realm.where(Department.class).equalTo("employees.name", "empName").findAll()

Objectify entity relationship

I'm beginner in Android and I'm trying to understand how GAE works with Objectify.
So I've created two classes, one 'User' and another one 'Journey'. Each Journey belongs to a User.
User Class
#Entity
public class User {
#Id
private Long id;
#Index private String mac;
private String name;
private String firstName;
private Long age;
private String email;
private String password;
// Getters and setters
}
Journey Class
#Entity
public class Journey {
#Id
private Long id;
Key<User> driver;
private Event event;
private Long nbPlaces;
private String departureTime;
private String destination;
}
I've writed the following method on the User class, is this correct ?
#Transient
Key getKey() {
return Key.create(User.class, id);
}
How can I set the Key of the User in my Journey Object ? (I think I can't use a simple setter.
Thanks !
Previously declared:
import static com.googlecode.objectify.ObjectifyService.ofy;
To get the object:
public User get(Long id) {
return ofy().load().key(Key.create(User.class, id)).now();
}
And to set the Key from User into Journey class, you need to pass into the constructor when the object is created, or get the object Journey set the parameters and save it. But you need to get the Key previously:
public Long getKey(User user) {
Key<User> generatedKey = ofy().save().entity(user).now();
return generatedKey.getId();
}
After this, you can attach a list of Users into Journeyclass.

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()

Android: Mapping JSON into RealmObject with nested JSON objects doesn't work

I have a JSON string that contains a nested json like
{
"name": "name",
...
...
"profile": {
"id": 987,
"first_name": "first name"
...
...
}
}
I'm trying to map this JSON into Realm by using the method realm.createObjectFromJson(Class clazz, String string) and the problem is that the nested JSON is not mapped, the resulting RealmObject instance that corresponds to the "profile" has 0's and null's for all the fields. I used realm.beginTransaction() before the create operation, and realm.commitTransaction() after.
I'm using 'io.realm:realm-android:0.80.1' for my Android project.
Can you please tell me what am I doing wrong?
Thanks.
EDIT
These are my model classes. Simple RealmObjects linked together
public class SomeClass extends RealmObject {
private String name;
private Profile profile;
public Profile getProfile() {
return profile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
}
public class Profile extends RealmObject {
private String firstName;
private String lastName;
private String birthdate;
private boolean newsLetter;
private boolean push;
private int userId;
private Date lastUpdate;
private RealmList<RealmAddress> addresses;
private RealmList<RealmGender> genders;
}
the profile class contains only getters and setters, and it contains other Strings and ints, which I deleted for the sake of simplicity.
Your JSON names doesn't match your child object field names which is why you don't see any data. Your profile name matches the field in SomeClass, which means the object gets created (with default values), but as none of the fields match in Profile, none of them are set.
firstName != first_name
userId != id
If you want to have separate names in your JSON and the Java models you should use something like GSON (http://realm.io/docs/java/#gson) as that is not yet supported by Realm directly.
use this :
public class Profile extends RealmObject {
private String first_name;
private int id;
...
}
check that you have the same names in JSON and your class model

Categories

Resources