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()
Related
ChatRoom:
public class ChatRoom extends RealmObject{
#PrimaryKey
int chatRoomID;
RealmList<ChatMessage> chatMessages;
//getters & setters ...
}
Message class:
public class ChatMessage extends RealmObject {
int chatMessageID;
String chatMessageContent;
User author;
//getters & setters ...
}
User:
public class User extends RealmObject {
#PrimaryKey
int Id;
String Name;
//getters & setters ...
}
Well the first time that the chatmessage is created is fine but in the second time I get an error: Value already exists: 5 [Id of the User]
How can I update the user object when is added to my chatmessage list instead of created a new one?
This is the way that I add a new chatMessage:
mRealm.beginTransaction();
ChatRoom chatRoom = mRealm.where(ChatRoom.class).equalTo("chatRoomID", chatRoomID)
.findFirst();
chatRoom.getChatMessages().add(chatMessage); // add to the list
mRealm.commitTransaction();
It is because of chatMessage is a unmanaged RealmObject and it has the same primary key with one is saved by Realm already.
So if an object is unmanaged object, when added it to a managed RealmList, Realm will try to create it. And an exception will be thrown when an object with the same pk already exists.
To solve this, use copyToRealmOrUpdate() to get a managed RealmObject first then add it, like:
ChatMessage ch = new ChatMessage();
ch.SetChatMessageID(someId);
ch.setChatMessageContent("text");
ch.setChatMessageAuthor(author);
mRealm.beginTransaction();
ch = mRealm.copyToRealmOrUpdate(ch); // get/create a managed RealmObject
ChatRoom chatRoom = mRealm.where(ChatRoom.class).equalTo("chatRoomID", chatRoomID)
.findFirst();
chatRoom.getChatMessages().add(ch); // add to the list
mRealm.commitTransaction();
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.
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.
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;
...
}
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