Reputation: 2646
I have a project where I am reading some json through GSON and Volley. I want to save my data in a database, and I was hoping that Realm would be a good solution. I took my first class which has seven member variables, all Strings and ints and had it extend RealmObject and identified one of the ints as the primary key. It compiles fine, but when it runs, I get tons and tons of output in the logs, and eventually the app just crashes before showing the main activity. It seems that GSON does not like parsing a class that has extended RealmObject.
So I did some searching and I found this post from the Realm site, but it is for an older version of Realm (I am currently using 0.87)
https://realm.io/docs/java/0.77.0/#other-libraries
It gives a solution, but I can't get that solution working for me because they are parsing their GSON differently that I am. Maybe we can solve this by just helping me here, but I wonder if this soltution won't work not matter since it is for an older version?? Anyway, I am parsing my GSON like this:
private void DisplayData(int count, final ArrayList<TwoLineSummaryCardDataObject> tlscdo,
final TwoLineSummaryViewAdapter lsva) {
final String url = "https://mydata.com/mydata";
final GsonRequest gsonRequest =
new GsonRequest(url, MyData.class, null, new Response.Listener<MyData>() {
@Override
public void onResponse(MyData myData) {
tlscdo.clear();
// Disable Realm stuff for now
//Realm realm = Realm.getDefaultInstance();
//realm.beginTransaction();
for (int i = 0; i < myData.getData().size(); i++) {
tlscdo.add(new TwoLineSummaryCardDataObject(myData.getData().get(i)));
//realm.copyToRealmOrUpdate(myData.getData().get(i));
}
//realm.commitTransaction();
lsva.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
if(volleyError != null) Log.e("MainActivity", volleyError.getMessage());
Toast.makeText(MainActivity.this, "Error in Volley", Toast.LENGTH_SHORT).show();
}
}
);
// Add the request to the queue
Volley.newRequestQueue(this).add(gsonRequest);
VolleyHelper.getInstance(getApplicationContext()).addToRequestQueue(gsonRequest);
1) How do I update my code so I can use a GsonBuilder like the example on the Realm javadoc page?
2) Am I wasting my time because that code is for an older version of Realm and there is now a different/better/no way to make GSON and Realm play together?
Edit: There was a req to see the MyData class. It's really simple:
public class MyData {
@PrimaryKey
private Integer id;
private String name;
private String abbrev;
private String countryCode;
private Integer type;
private String infoURL;
private String wikiURL;
// followed by a bunch of getters and setters
}
To clarify, it runs fine like this. If I add "extends RealmObject", it will still compile, but when running, it just spews out all kinds of messages and then the app eventually crashes (out of memory, I presume) after a few seconds without ever showing the activity.
Edit 2, adding in the logcat as requested.
When I do run it with the "extends RealmObject", this is the logcat.
http://pastebin.com/raw/1VZq8bQD
And if I take the "extends RealmObject" out, it runs perfectly.
Upvotes: 0
Views: 521
Reputation: 884
1) How do I update my code so I can use a GsonBuilder like the example on the Realm javadoc page?
You can create a new class that extends Request<T>
and apply it to your existing code. In your case, the quickest way is to copy the GsonRequest
class source code to a new one, and modify the line that intialize the Gson
object in the constructor:
mGson = new Gson();
with the code as written in realm.io's website.
2) Am I wasting my time because that code is for an older version of Realm and there is now a different/better/no way to make GSON and Realm play together?
As of writing this answer, the lastest version of realm is 0.87.1, and the method of working with GSON is the same. So this should still be the proper way.
Upvotes: 1