Are there arithematic operations in Realm for querying data?
No, only if you save such results yourself as a field 🤔
I didn't get you , can you explain a little bit @Zhuiden
See the total example in https://link.medium.com/2utWr5wVuT
I have two Models for example
One is InventoryOutModel(For sending the inventory Outside)
Second is inventoryPresentModel(For inventory which is now left)
What i am trying to do that when i send some quantity from inventoryOutModel then it will automatically be subtracted from present Model.
How to do that?
You write the transaction that modifies the quantity field of the objects you want to modify
Can you show me an example?
You cannot do it in queries, but you can just create a custom method that does it:
public class Foo extends RealmObject {
int val1;
int val2;
int getResult() {
return val1 - val2;
}
}
You would use a similar pattern for inserting data so you always update the fields together.
This is my InventoryOut Model...
import java.util.UUID;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.annotations.PrimaryKey;
public class inventoryOutModel extends RealmObject {
@PrimaryKey
private String id = UUID.randomUUID().toString();
private String outName;
private String outFeild;
private double outQuantity;
private String outDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getOutName() {
return outName;
}
public void setOutName(String outName) {
this.outName = outName;
}
public String getOutFeild() {
return outFeild;
}
public void setOutFeild(String outFeild) {
this.outFeild = outFeild;
}
public double getOutQuantity() {
return outQuantity;
}
public void setOutQuantity(double outQuantity) {
this.outQuantity = outQuantity;
}
public String getOutDate() {
return outDate;
}
public void setOutDate(String outDate) {
this.outDate = outDate;
}
}
This is inventoryPresentModel
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class inventoryPresentModel extends RealmObject {
@PrimaryKey
private String id;
private String presentName;
private double presentQuantity;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPresentName() {
return presentName;
}
public void setPresentName(String presentName) {
this.presentName = presentName;
}
public double getPresentQuantity() {
return presentQuantity;
}
public void setPresentQuantity(double presentQuantity) {
this.presentQuantity = presentQuantity;
}
}
I want to subtract values of quantity like outQUantity - presentQuantity
You'd need a third RealmObject with the required values calculated inside it and a reference to both the PresentModel and the OutModel.
I have also inventoryIn model for incoming inventory , in that model there is quantity which i dont want to change
@cmelchior and @zhuinden
I mean. It's up to you to write the logic for this behavior that you want/need.
But there are no obstacles for it either. It's just that you need to do it with Java(/Kotlin) and Realm doesn't have an update set blah = a+b, so it's up to you to do it.
You have a executeTransaction { block and you can do whatever you want. Go nuts.
@Zhudien sorry if you get angry @ me , but seriously i am not getting you...your level is more advanced
But you're the one with the complete schema and the behavioral reqs, so we can't write it for you :wink:
p.s. I'm not angry, I'm just saying "this is what Realm knows, this is what it doesn't, and you'll have to connect the dots to get what you need"
ultimately, the short answer to your original question is "no, Realm does not support any arithmetical operations inside Realm, you need to do this stuff in Java"
Glad to hear @Zhudien
@cmelchior you are right but how to subtract values from two different Models?
For what it's worth, realm-core has support for basic arithmetic expressions (example), but I don't think any binding has support for it because there have been very few requests to use a feature like this. Like @Zhuinden mentions, until this is prioritised and implemented, you can always do a bit more work on the app side and perform the calculations manually.
You cannot do it in queries, but you can just create a custom method that does it:
public class Foo extends RealmObject { int val1; int val2; int getResult() { return val1 - val2; } }You would use a similar pattern for inserting data so you always update the fields together.
The getter function is returning null value?
The getter function is returning null value?
It cannot since the type is int, but it might look wrong i you are checking in a debugger: https://realm.io/docs/java/latest/#testing-and-debugging
Nope i am not checking in debugger ...
Closing due to inactivity. If this is still a problem please reopen the issue.
Most helpful comment
But you're the one with the complete schema and the behavioral reqs, so we can't write it for you :wink:
p.s. I'm not angry, I'm just saying "this is what Realm knows, this is what it doesn't, and you'll have to connect the dots to get what you need"
ultimately, the short answer to your original question is "no, Realm does not support any arithmetical operations inside Realm, you need to do this stuff in Java"