Today I moved to Realm 0.83 and it is nice that we have null support but I have a problem.
I want to fetch all the stores that have empty products list inside. So far it worked if I used isNull() on the RealmQuery but since the update I get a crash like: Illegal Argument: RealmList is not nullable.
As it states in the crash, I cannot do this anymore because a RealmList is a Required field from now on so it can't be empty.. ok, that is nice but what can I use on the RealmQuery to fetch the models that I want?
Thank you!
The issue has been resolved by realm. You can now use isEmpty and isNotEmpty in the query builder for all RealmList properties.
Unfortunately, there is no option for doing that exact query anymore in 0.83.0. We think the improved isNull semantics are better, but it is very unfortunate that it is breaking current behaviour. I have created an issue for adding support back for this and hope to have it resolved very soon: https://github.com/realm/realm-java/issues/1601.
Right now you will have to work around it by manually iterating your data to find all objects that match your criteria.
Related
I am triying to delete a field of an object in Back4App, but I cannot achieve such a simple operation. By "deleting" I mean set a field that has data to "undefined".
According to the guide, I just have to call myObject.remove("field"). I tryed that (with correct field name), then save the object (I tried all of the saving functions available), but the object is unmodified. There is no error thrown.
I can change the field (with put ("field", otherObject), because it is a pointer field) with no problem. But put("field", JSONObject.NULL) is not working either.
I do not know if this code would work in the original Parse, I am coding this now. The equivalent function in iOS ([myObject removeObjectForKey:#"field"];) in the same database is working nicely...
For what I could gather from your question, the problem is that you're trying to clean a field from a relational object:
"I can change the field (with put ("field", otherObject), because it is
a pointer field)"
On that case, I'm not really sure if using simple object deletion would work. I'd suggest you to take a look at Parse's documentation on Relational Data in order to understand how you should remove that field.
Long story short, I'm not sure if the idea of cleaning the field that you wish will work, but what can be done when you have a relation like this:
ParseUser user = ParseUser.getCurrentUser();
ParseRelation<ParseObject> relation = user.getRelation("field");
relation.add(MyObject);
user.saveInBackground();
Is to simply remove the relation like this:
relation.remove(MyObject);
As you can check in the link above.
I'm using greenDAO 3.1 for one of my projects. Since I needed my id to be UUID I've decided to store it as ByteArray. Now the problem is I can't update my entities using update or updateInTx method and I have to use insertOrReplace or insertOrReplaceInTx method.
Can anybody tell me what is going on and why can't I update using update methods?
Is there any downside to using insertOrReplace methods instead of update methods?
This is my Entity's schema code:
Entity book = schema.addEntity("Book");
book.addByteArrayProperty("id").columnName("_id").primaryKey();
book.addStringProperty("title");
book.addByteProperty("edition");
book.addStringProperty("authors");
book.addStringProperty("desc");
book.addStringProperty("pic");
And here's my update code:
BookDao bookDao = daoSession.getBookDao();
List<Book> books = bookDao.loadAll();
for (Book book : books)
book.setDesc("It doesn't really matter!");
bookDao.updateInTx(books); //This isn't working
After a lot of searching and trouble I found the reason.
Since you can't query BLOB type in sqlite and since update methods use a condition over id in WHERE clause, the update method won't find the record I'm looking for. On the other hand when I use one of insertOrReplace methods, since the id field is unique it can't insert redundant id to the table and it completely replaces the old record with the one I'm trying to update. So, there was no problem with update methods, the real problem is with using ByteArray as id.
For the same reason, I also encountered an error when selecting using load method.
I'm trying to use Realm to store a local database of objects. The app checks if the current session is first load and if so populates the local database with an api call. But, if the database is not empty, I would like to use the data already available. To do this, I need to know whether the database is empty or not.
I found this issue on github, but they dont provide a workaround:
https://github.com/realm/realm-java/issues/766
So how should this be done?
If you scroll down that issue page, you can see Realm.isEmpty() is added. :)
Add a realm.isEmpty() method that returns true if there is no objects
in the Realm. It is just a utility method, but fits nicely with the
Realm object store abstraction.
use realm.isEmpty()
I used
if(realmResults.isEmpty()) {action...}
or
if(realmResults.isNullOrEmpty()) {action...]
there's false but action..
What's wrong!? ...T0T
When using copyToRealmOrUpdate it also overrides fields with existing values. I would expect it would only update the fields I gave and use the existing values for the other fields.
I saw this issue for createOrUpdateFromJson: https://github.com/realm/realm-java/issues/933
cmelchior says this:
It is impossible to tell the difference between an value not set and
it's default value, so there it should override all properties.
I wanted to create an issue with label enhancement for realm, but instead I ask it here first. Is it really impossible? Because it would be a great improvement to me.
Thanks!
Note there is difference between using Realm.copyToRealmOrupdate(RealmObject) and Realm.createOrUpdateFromJson(Json)
The answer I gave is true for copyToRealmOrUpdate() eg. you cannot tell the difference between the following in Java:
boolean bool1;
boolean bool2 = false;
It is different for JSON where you can tell if a property is missing altogether. However the current implementation doesn't work that way. We are currently in process of merging a Pull Request that actually has the behaviour you are looking for. You can follow the progress here: https://github.com/realm/realm-java/pull/1022
i have been searching for a while for a solution to my problem without success.
I have an application in which I receive information for a particular entity in my database from different services, so I am using greenDAOs insertOrReplace methods so whenever the entity already exists in my DB it gets updated instead of recreated.
So far so good.
The problem is.. let's say for example sake my entity is called User, with fields id, title, and displayName.
So in the first call I get a JSON object containing a user with only its id and title fields, so I insert it into the DB and naturally the displayName gets inserted as NULL.
Afterwards from another service I get another JSON containing the same user (same id field), but it comes with the displayName as well, but doesn't include the title info at all.
So whenever I run the insertOrReplace on the DAO object automatically generated by greenDAO, the user gets updated but as the title info was not present, when it gets updated the title field gets reset to NULL, so I end up losing data.
Unfortunately I am unable to change the data being returned from the services, and haven't been able to fix this issue. I find it hard to believe there is no easy way to tell the DAO object to update only certain fields and no all of them.
I was looking at the code generated by greenDAO and in the dao objects generated there is a bindValues method which gets called before the query gets executed, and apparently it filters out the NULL properties from the object, but either way it gets updated with the NULL value.
I was able to come up with some sort of fix by modifying the final dao object being generated by adding some methods from the parent class, but I don't think this is a good solution because I would have to do this for all the dao objects. (I know it's possible to define a custom superclass but this only applies for the entity object and not the DAO one).
I would really appreciate if someone has any idea on how I could resolve this, and sorry for the long explanation, I just wanted to be clear on my issue.
Thanks.
First of all: I wouldn't tamper with the generated code unless you really know what you are doing. Modifications may have effects on caches and data-integrity.
Generally you are following this (insert-or)-update-approach if you are using a ORM-Framework (like greendao):
Try to get the entity, that you want to modify from the db (maybe it is already in cache, so this may not be a real database operation)
If you don't have such an entity: create it
Modify the entity according to your needs
Insert or Update it in database (in greendao you would use insertOrReplace)