Handling ForeignCollection when manually initializing parent entity - android

I designed a model with two entities : ParentEntity and ChildEntity. I use OrmLite to store them in a database.
I actually get my data from a remote webservice. This is the process I have :
Request the webservice (using retrofit)
Get JSON string in response
JSON string is parsed to JSON model by Gson (thank you again retrofit :))
I convert JSON model to an OrmLite model (no library, I make it myself)
The OrmLite model is given back to the callback waiting for the response of the request
The callback is in charge of calling the DAO to actually store the data
This process works perfectly for simple entities. But a problem appears when I try to manage more complex entities, with a ForeignCollection for example. Actually, I can't achieve the step 4 because I can't create a new ForeignCollection to put my child entities inside it.
I found some answers saying that I should store every child myself, but it will break the step 6 of my workflow.
So the question is :
How can I initialize the ForeignCollection before getting the object from the database ?
I could find a way to change the workflow. But it's only a kind of "work-around" and will create container objects just to achieve this...
The OrmLite schema (simplified)
(The class Entity just have a property id and its getter/setter.)
ParentEntity
#DatabaseTable(daoClass = ParentEntityDao.class)
public class ParentEntity extends Entity
{
#ForeignCollectionField(eager = true)
private ForeignCollection<TimesheetEntry> entries;
public Collection<TimesheetEntry> getEntries()
{
if(entries != null)
{
return Collections.unmodifiableCollection(entries);
}
else
{
return Collections.unmodifiableCollection(new ArrayList<TimesheetEntry>());
}
}
public void addEntry(ChildEntry childEntry)
{
childEntry.setParent(this);
entries.add(childEntry);
}
public void removeEntry(ChildEntry childEntry)
{
entries.remove(childEntry);
}
}
ChildEntity
#DatabaseTable(daoClass = ChildEntityDao.class)
public class ChildEntry extends Entity
{
#DatabaseField(foreign = true, canBeNull = false)
private ParentEntity parentEntity;
public void setParentEntity(ParentEntity parentEntity)
{
this.parentEntity = parentEntity;
}
public ParentEntity getParentEntity()
{
return parentEntity;
}
}
JSON schema (simplified)
Parent entity
public class JsonParentEntity
{
private List<JsonChildEntity> entries;
// Getter/setter
}
Child entity
public class JsonChildEntity
{
private String name;
// Getter/setter
}

Related

How to retrieve correct subtype from firebase android getValue()

Hi all I can't think of a better example to illustrate my point so do let me know If my example has some errors. But hopefully this example will get my point through.
class A {
String CATEGORY = "A";
public String getCATEGORY() {
return CATEGORY;
}
}
class B extends A {
String CATEGORY = "B";
#Override
public String getCATEGORY() {
return CATEGORY;
}
}
class C extends A {
String CATEGORY = "C";
#Override
public String getCATEGORY() {
return CATEGORY;
}
}
public class MyClass {
private List<A> array = Arrays.asList(new A(), new B(), new C());
public MyClass() {}
}
Now if I upload MyClass onto firebase using setValue for example, firebase will show me the properties of class A, B and C. However, when I read the data from firebase and call sth like getValue(MyClass.class) the List it returns me are all of type A and the subclasses are not preserved. Is there a workaround to allow firebase to preserve the class types uploaded?
If you use Firebase's default serializer, it simply writes all public properties and fields to the database. Say that you store a single instance of each class, it'd be:
-L1234567890: {
cATEGORY: "A"
},
-L1234567891: {
cATEGORY: "B"
},
-L1234567892: {
cATEGORY: "C"
},
There won't be enough knowledge in the database for the SDK to reinflate the correct sub-class. While you and I can see that the cATEGORY value matches the class name, the Firebase SDK has no such knowledge.
It won't be too hard to write your own custom deserializer for this data though, taking a DataSnapshot with the values above and reinflating the correct class and values.
You could also do a hybrid: detect the class type directly, and then tell Firebase what class to read:
String cat = snapshot.child("cATEGORY").getValue(String.class)
Class clazz = "C".equals(cat) ? C.class : "B".equals(cat) ? B.class : A.clas;
A object = snapshot.getValue(clazz);

Query realm data contained on other object

This question is a follow-up question from: Organize Android Realm data in lists
Due to the data returned by the API we use, it's slightly impossible to do an actual query on the realm database. Instead I'm wrapping my ordered data in a RealmList and adding a #PrimaryKey public String id; to it.
So our realm data looks like:
public class ListPhoto extends RealmObject {
#PrimaryKey public String id;
public RealmList<Photo> list; // Photo contains String/int/boolean
}
which makes easy to write to and read from the Realm DB by simply using the API endpoint as the id.
So a typical query on it looks like:
realm.where(ListPhoto.class).equalTo("id", id).findFirstAsync();
This creates a slightly overhead of listening/subscribing to data because now I need to check listUser.isLoaded() use ListUser to addChangeListener/removeChangeListener and ListUser.list as an actual data on my adapter.
So my question is:
Is there a way I can query this realm to receive a RealmResults<Photo>. That way I could easily use this data in RealmRecyclerViewAdapter and use listeners directly on it.
Edit: to further clarify, I would like something like the following (I know this doesn't compile, it's just a pseudo-code on what I would like to achieve).
realm
.where(ListPhoto.class)
.equalTo("id", id)
.findFirstAsync() // get a results of that photo list
.where(Photo.class)
.getField("list")
.findAllAsync(); // get the field "list" into a `RealmResults<Photo>`
edit final code: considering it's not possible ATM to do it directly on queries, my final solution was to simply have an adapter that checks data and subscribe if needed. Code below:
public abstract class RealmAdapter
<T extends RealmModel,
VH extends RecyclerView.ViewHolder>
extends RealmRecyclerViewAdapter<T, VH>
implements RealmChangeListener<RealmModel> {
public RealmAdapter(Context context, OrderedRealmCollection data, RealmObject realmObject) {
super(context, data, true);
if (data == null) {
realmObject.addChangeListener(this);
}
}
#Override public void onChange(RealmModel element) {
RealmList list = null;
try {
// accessing the `getter` from the generated class
// because it can be list of Photo, User, Album, Comment, etc
// but the field name will always be `list` so the generated will always be realmGet$list
list = (RealmList) element.getClass().getMethod("realmGet$list").invoke(element);
} catch (Exception e) {
e.printStackTrace();
}
if (list != null) {
((RealmObject) element).removeChangeListener(this);
updateData(list);
}
}
}
First you query the ListPhoto, because it's async you have to register a listener for the results. Then in that listener you can query the result to get a RealmResult.
Something like this
final ListPhoto listPhoto = realm.where(ListPhoto.class).equalTo("id", id).findFirstAsync();
listPhoto.addChangeListener(new RealmChangeListener<RealmModel>() {
#Override
public void onChange(RealmModel element) {
RealmResults<Photo> photos = listPhoto.getList().where().findAll();
// do stuff with your photo results here.
// unregister the listener.
listPhoto.removeChangeListeners();
}
});
Note that you can actually query a RealmList. That's why we can call listPhoto.getList().where(). The where() just means "return all".
I cannot test it because I don't have your code. You may need to cast the element with ((ListPhoto) element).
I know you said you're not considering the option of using the synchronous API, but I still think it's worth noting that your problem would be solved like so:
RealmResults<Photo> results = realm.where(ListPhoto.class).equalTo("id", id).findFirst()
.getList().where().findAll();
EDIT: To be completely informative though, I cite the docs:
findFirstAsync
public E findFirstAsync()
Similar to findFirst() but runs asynchronously on a worker thread This method is only available from a Looper thread.
Returns: immediately an empty RealmObject.
Trying to access any field on the returned object before it is loaded
will throw an IllegalStateException.
Use RealmObject.isLoaded() to check if the object is fully loaded
or register a listener RealmObject.addChangeListener(io.realm.RealmChangeListener<E>) to be
notified when the query completes.
If no RealmObject was found after
the query completed, the returned RealmObject will have
RealmObject.isLoaded() set to true and RealmObject.isValid() set to
false.
So technically yes, you need to do the following:
private OrderedRealmCollection<Photo> photos = null;
//...
final ListPhoto listPhoto = realm.where(ListPhoto.class).equalTo("id", id).findFirstAsync();
listPhoto.addChangeListener(new RealmChangeListener<ListPhoto>() {
#Override
public void onChange(ListPhoto element) {
if(element.isValid()) {
realmRecyclerViewAdapter.updateData(element.list);
}
listPhoto.removeChangeListeners();
}
}

Does Retrofit support keypath on json parsing or something alike?

For example i have json looks like:
{
"data": [
{
"name": "one"
},
{
"name": "two"
}
]
}
For example i have object User with field name.
Is it possible write method which will parse data array to objects User?
something like
Call<List<User>> getUsers(#KeyPath("data"))
Now to do this, i need create a wrapper class something like
public class UsersWrapper {
#SerializeName("data")
public ArrayList<User> users;
}
and in service i do next
public interface Service {
#GET("users")
Call<UsersWrapper> getUsers()
}
But my all requests is just response with data but variable objects in array.
In this case i need create wrappers to any data requests. Pain :(
?
I'd do it this way:
Global class Wrapper<T> to parse the whole JSON
public class Wrapper<T> {
public List<T> data;
}
And User to parse actual array;
public class User {
public String name;
}
Then, the API interface:
#GET("/people")
Wrapper<User> getUsers();
And in DataSource class just do something like this:
#Override
public List<User> getUsers() {
Wrapper<User> usersWrapper = myApiInterface.getUsers();
return usersWrapper.data;
}
Upd1:
Another solution is to create custom JsonDeserializer (like described here) for List<User> type, register by registerTypeAdapter it with your custom Gson object and then you can deserialise your Json directly into List<User>. Though, this solution brings much more extra code and potential benefit is unclear for me.

Practical use of #Ignore in Realm?

I've been trying to add Realm in my Android app. Their docs are pretty well explained & easy to follow. But it fails to explain this one particular area. I'm unable to figure out the practical use for the #Ignore annotation. I know that fields under this annotation are not persisted.
Can someone please share a few use cases. Also I wanted to know the scope of such fields. I mean, if I set an #Ignore field to some value, would that value be available to the other classes in my app for that particular launch session. If yes, then how do we access it? If no (which I guess is the case), then why do we need such a field anyway?
I've searched here and on web but couldn't find the relevant information. If out of my ignorance, I've missed upon some resource, please guide me to it.
Thanks.
Accordingly to the official documentation (see https://realm.io/docs/java/latest/) #Ignore is useful in two cases:
When you use GSON integration and your JSON contains more data than you want to store, but you still would like to parse it, and use right after.
You can't create custom getters and setter in classes extending RealmObject, since they are going to be overridden. But in case you want to have some custom logic anyway, ignored fields can be used as a hack to do that, because Realm doesn't override their getter & setters. Example:
package io.realm.entities;
import io.realm.RealmObject;
import io.realm.annotations.Ignore;
public class StringOnly extends RealmObject {
private String name;
#Ignore
private String kingName;
// custom setter
public void setKingName(String kingName) { setName("King " + kingName); }
// custom getter
public String getKingName() { return getName(); }
// setter and getter for 'name'
}
Ignored fields are accessible only from the object they were set in (same as with regular objects in Java).
UPDATE: As the #The-null-Pointer- pointed out in the comments the second point is out of date. Realm now allows having custom getters and setters in Realm models.
Here's a couple of real-world use cases:
1 - Get user's fullname:
public class User extends RealmObject {
private String first;
private String last;
#Ignore
private String fullName;
public String getFullName() {
return getFirst() + " " + getLast();
}
Get JSON representation of object:
public class User extends RealmObject {
private String first;
private String last;
#Ignore
private JSONObject Json;
public JSONObject getJson() {
try {
JSONObject dict = new JSONObject();
dict.put("first", getFirst());
dict.put("last", getLast());
return dict;
} catch (JSONException e) {
// log the exception
}
return null;
}
I've found it useful to define field names for when I am querying. For example
User.java
public class User extends RealmObject {
#Index
public String name;
#Ignore
public static final String NAME = "name";
}
And then later on I can do something like:
realm.where(User.class).equalTo(User.NAME, "John").findFirst();
This way if the schema changes from say name to id I don't have to hunt down every occurrence of "name".
Please see the the official documentation about #Ignore annotation:
The annotation #Ignore implies that a field should not be persisted to disk. Ignored fields are useful if your input contains more fields than your model, and you don’t wish to have many special cases for handling these unused data fields.

Data change in realm android

In my android app, I persist a workout object to realm. In one of my activities, I create an object with this code:
realm.beginTransaction();
Workout w = realm.createObject(Workout.class);
w.setmWorkoutId(UUID.randomUUID().toString());
realm.commitTransaction();
Here is my workout class:
public class Workout extends RealmObject{
private String mWorkoutId;
private int restSecsLeft;
private boolean prevSetOver = true;
private boolean workoutOver = false;
public Workout(){}
public String getmWorkoutId() {
return mWorkoutId;
}
public void setmWorkoutId(String mWorkoutId) {
this.mWorkoutId = mWorkoutId;
}
public int getRestSecsLeft() {
return restSecsLeft;
}
public void setRestSecsLeft(int restSecsLeft) {
this.restSecsLeft = restSecsLeft;
}
public boolean getPrevSetOver() {
return prevSetOver;
}
public void setPrevSetOver(boolean prevSetOver) {
this.prevSetOver = prevSetOver;
}
public boolean getWorkoutOver() {
return workoutOver;
}
public void setWorkoutOver(boolean workoutOver) {
this.workoutOver = workoutOver;
}
}
I have a service that runs after a workout is created, and after debugging odd behavior, have found an instance where the value of prevSetOver that is saved in a workout RealmObject is different than the value returned from w.getPrevSetOver(). I am not sure how this is happening--I do not change the value of the variable prevSetOver after an object is instantiated. I am a new realm user and do not understand how this is happening. The picture I have attatched is a screenshot of the w.prevSetOver() method and the RealmObject having different values.
There are more variables in the debugger in this screen, I left most of them out in my post for simplicity's sake.
It is the right behaviour of Realm.
Realm generates Proxy object which inherit from your Workout when compiling. And read/write data from/to Realm is actually implemented by the Proxy Object through overriding getters/setters. The original Object's member field won't be changed by Realm.
When Realm.createObject() get called, it does return a Proxy object, whose member fields are not what you expected.
You still can create a instance of the original model object which we call it standalone object (means it is not managed by Realm) by calling Workout w = new Workout(). This would act just like normal Java object. And you still can copy it to Realm by calling w = realm.copyToRealmOrUpdate(w). Notice we changed the w's value to the return value. The function will return a Proxy object which is managed by Realm now.
realm.beginTransaction();
Workout w = realm.createObject(Workout.class);
w.setmWorkoutId(UUID.randomUUID().toString());
realm.copyToRealm(w); //<-- u need
realm.commitTransaction();
is better add Primarykey, changed u String variable to long
#PrimaryKey
private long mWorkoutId;

Categories

Resources