Following is Pojo are extended by realm , at first time getting all fields from api and i am using insertOrUpdate() operation of realm to dump data in realm.
ServiceModel.java
#JsonProperty("id")
private String id;
#JsonProperty("title")
private String title;
#JsonProperty("description")
private String description;
#JsonProperty("current_status")
private String currentStatus;
But for next time i am not getting current_status field from response and so my ServiceModel table is getting updated with currentStatus as null. How To make currentStatus to not getting updated if it is not in response.
I had the same problem and here how I solved it. You should select existing Object by id:
ServiceModel existingService = realm.where(ServiceModel.class).equalsTo("id",id).findFirst();
if(existingService !=null){ // check if such record existed, it may be absent
String oldCurrentStatus = existingService.currentStatus();
//set old current status to new API object
}
I had the same problem and as far as I know there is no Realm function or annotation yet for such case.
Related
I have an app that stores lots of data to work offline as well.
I have three classes, in a hierarchy like;
public class MainGroup
{
private UUID Oid;
private String name;
private Date CreatedOn;
}
-
public class Group
{
private UUID Oid;
private String name;
private Date CreatedOn;
private MainGruop MainGroup;
}
-
public class Product
{
private UUID Oid;
private String name;
private Date CreatedOn;
private MainGruop MainGroup;
private Group Group;
}
( Oid fields are selected as PrimaryKey with realm attribute. )
Let's say, all MainGroup objects were stored in Realm DB. Then, when i'm trying to insert Group objects, with nested MainGroup object but with only its Oid field to link its master, Realm updates the MainProduct record (with given Oid), and clear the other fields as nulls.
In same way, when i'm inserting Product objects and nested objects are includes only Oid, realm updates all fields with nulls.
So, there are more complex and deeply related objects and when i make a request to get JSON from server, i must produce a very big JSON response to keep data.
And mention to insert method; I'm creating java objects with JSON response via GSON and i'm using Realm.copyToRealmOrUpdate(obj); method to insert.
To reduce payload (JSON size, serialize and insertion process), i need to find a way to fix this issue.
I am working on some chat - app whith complex messages.
Messages from API stored in realm DB.
When i getting messages: call after API response, deserialization finished, messages correct:
realm.beginTransaction();
realm.copyToRealmOrUpdate(listOfBaseMessageResponses);
realm.commitTransaction();
messages in db replaced and i forced to make a List in my activity and try to handle changes by myself.
Changes tracked in activity by:
messagesChangedListener = new RealmChangeListener() {
...
}
mBaseMessageResponsesRealm = realm.allObjects(BaseMessageResponse.class);
mBaseMessageResponsesRealm.addChangeListener(messagesChangedListener);
Way to get messages:
return realm.allObjects(BaseMessageResponse.class);
BaseMessageResponse class(getters and setters exists. for better understanding not show):
#SerializedName("clr")
#Expose
#PrimaryKey
private String clr;
#SerializedName("cap")
#Expose
private String cap;
#SerializedName("eoc")
#Expose
private int eoc;
#SerializedName("list")
#Expose
private RealmList<MessageInResponse> list = new RealmList<MessageInResponse>();
...
Also MessageInResponse contain nested classes.
Why messages replaced? I try many ways to figure it out but no result.
May be some problems with PK and logic for copyToRealmOrUpdate method?
I have two classes
City class
private int id;
private String cityName;
Person class
private int id;
private String personName;
private long dob;
private City hometown;
I want to know how I can get a list of people who are living in a particular city
My first attempt was this
RealmResults <Person> people = realm.where(Person.class).equalTo("hometown", "(1, Melbourne)").findAll();
When I execute this line I get following error
java.lang.IllegalArgumentException: Field 'hometown': type mismatch. Was STRING, expected LINK
Therefore, I want to know how I can pass a LINK as the second argument in "equalTo()" method.
RealmResults <Person> people = realm.where(Person.class).equalTo("hometown.cityName", "Melbourne").findAll();
I am using OrmLite 4.46 to manage my database for my android application.
And i have one problem:
I have the following code for my model:
public class Item extends Model {
#DatabaseField(generatedId = true)
private long id;
#DatabaseField(columnName = "item_name", defaultValue = "")
private String name;
#DatabaseField(columnName = "item_count", defaultValue = "0")
private int count;
public Item() {
super();
}
}
And the problem is here :
#DatabaseField(columnName = "item_name", defaultValue = "")
private String name;
When i am creating a new Item() with no arguments and i save it in the database, normally in the column item_name it should save an empty String.
But when i am retrieving the item from the database and i try
String itemName = item.getName().trim() I get a NullPointerException
So it seems that the name is null.
Also i checked the created table from the above model in the sqlite db file and when i set defaultValue="" the column is created with no default value.
Does anybody know of any solution to this problem?
You can initialize name field to empty string at class level like:
#DatabaseField(columnName = "item_name", defaultValue = "")
private String name = "";
This will initialize name field to empty string when you create object of class Item and save it to database.
When you retrieve it from database the name field will be empty ("") instead of null.
When i am creating a new Item() with no arguments and i save it in the database, normally in the column item_name it should save an empty String.
Although #Rakesh's answer is correct, this turns out to be a bug in ORMLite. The code is supposed to insert the defaultValue during a create if the field is null. However, there was an errant .equals("") that was stopping this.
I've submitted the following bug report and have fixed it in trunk. It will be in 4.47.
When writing an instance of my data class to the database via ORMLite, and one of the child members (a foreign field) is null, I get back a non null child member.
Data classes as follows:
public class Site {
// snip
#DatabaseField(foreign = true, canBeNull = true)
private InstallationType installationType;
}
public class InstallationType {
#DatabaseField(generatedId = true)
private int id;
#DatabaseField
private String name;
}
When I read my instance of the Site class again via
getSiteDao().queryForId(id);
the installationType member is non null, but with a non-existent id. The only way the rest of our application can now work with this object, is if I manually do a lookup through the InstallationTypeDAO and set what I get back on the site. Query will sometimes return null as per the documentation.
Is there a way of getting ORMLite to set this member to null?
This was a bug in ORMLite that was fixed in version 4.15 (3/7/2011). Here's the change log file. What version are you using? Have you tried to update? Here's the bug report page:
Currently the following test passes so I think we have good coverage on that bug.
#Test
public void testForeignNull() throws Exception {
Dao<Foreign, Integer> dao = createDao(Foreign.class, true);
Foreign foreign = new Foreign();
foreign.foo = null;
assertEquals(1, dao.create(foreign));
Foreign foreign2 = dao.queryForId(foreign.id);
assertNotNull(foreign2);
assertNull(foreign2.foo);
}
With Foreign having the following fields:
#DatabaseField(generatedId = true)
public int id;
#DatabaseField(foreign = true)
public Foo foo;
If you are up to date in versions, please let me know if you can change the test to get it to fail.