RealmList<String> contains specified value - android

I have a RealmObject named Activity:
public class Activity extends RealmObject {
#PrimaryKey
public String token;
public RealmList<String> names;
public RealmList<String> units;
public RealmList<Float> points;
}
When querying, I want to return all Activity items which their names list contains some value.(not a exactly matches, much like a SQL like statement.)
I tried
val allActivities = realm.where(Activity::class.java).contains("names", str).findAllAsync()
But I got an exception:
java.lang.IllegalArgumentException: Invalid query: field 'names' in
class 'Activity' is of invalid type 'STRING_LIST'.
I have tried How do I query RealmObject that have RealmList that contains specified value, but the type in the mentioned RealmList is extending RealmObject, while String is not. Any thoughts?

Related

Firebase cannot find setter if the setter is an overridden method

I have a base class that stores a field called updatedAt and a setter setUpdatedAt. There is a child class Child. I know that Firebase cannot find setter in the parent classes, so I just define the setter setUpdatedAt again in Child, but I am still getting "No setter/field for updatedAt found on class xxx.Child"
My database json has that "updatedAt" field.
Sample code:
public abstract class Model {
private String mId;
private long mUpdatedAt;
public String getId() {return mId;}
public void setId(String id) { mId = id;}
public long getUpdatedAt() {return mUpdatedAt;}
public void setUpdatedAt(long updatedAt) { mUpdatedAt = updatedAt;}
}
public abstract class Child extends Model {
private String mCreator;
public String getCreator() {
return mCreator;
}
public void setCreator(String creator) {
mCreator = creator;
}
// having the following or not does not change the outcome
public void setUpdatedAt(long updatedAt) {
super.setUpdatedAt(updatedAt);
}
}
// then somewhere else, do this
new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
model = dataSnapshot.getValue(Child.class);
}
And I got "No setter/field for updatedAt found on class xxx.Child" and "No setter/field for id found on class xxx.Child" when "model = dataSnapshot.getValue(Child.class);" is executed.
For actual code sample, see github MinFirebaseApp. In MainActivity, you can see the two methods "createEmployee" and "showEmployee", which shows how an employee is created in "createEmployee" and the same employee is fetched in "showEmployee". Using the code, you can reproduce it by (after you change the package id to yours):
Put the google-services.json file in the app folder
Start the app
Click on "login" to sign in anonymously
Click on "create employee", see that the employee is created
Go to console.firebase.google.com to check that the employee is
created with id, name and updatedAt
Click on "show employee" from the app, see the "updatedAt" and "id"
are not set.
Check the logcat to find something like "No setter/field for id found" and "No setter/field for updatedAt found"
You'll need to call through to all of your setters in the subclass. You may as well do the getters too. Right now you're just doing setUpdatedAt(), but there is setId() as well. This is a bug in the Android client library for Realtime Database, but the team is working on a fix that will be available in the future.

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

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;
...
}

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