Realm or Paper for JPA in Android?

I am developing an Android application with Android Annotations . For persistence, I first used Content Provider (very complex) on top of SQLite. Then I discovered the Kingdom. It seemed to me to be pretty cool until I had to be notified of inserts to make my RecyclerView dynamic. To receive insert notifications, I made one class, which I named RealmProxy, with a proxy method for copyToRealm()

and an interface for implementation RealmListener

. I called the registered listeners in my method copyToRealm()

, passing them the added RealmObject, so I could populate the SortedList (list of supported libraries targeting RecyclerView) of the RecyclerView Adapter. I also used myRealmListener

an interface for sending new objects over the network as soon as they are saved.

After compiling and running, I got and IllegalStateException

(Accessing the Realm from the wrong thread. Realm objects can only be accessed on the thread they were created.) Because I am getting the Realm instance from the UI thread, but I am sending them over the network in the background stream. Why am I getting this error? Whenever my JSON serialization library LoganSquare , based on Jackson calls a getter on mine RealmObject

in the background to send over the network, this exception is thrown. This made me hate Realm's threading policy and the fact that fine-grained notifications aren't built in. Also, Realm doesn't let me define any custom method. I can't even implement Comparable

in Realm object.

When I saw Paper (thanks to Android Arsenal and Pushbullet), today I was very interested in a JPA solution without the headache. It seems very simple, no limitation for lists, maps and any class not propagating a custom class (Realm requires a RealmObject extension and use RealmList instead of a generic list which my json <> java didn't like, making me copy lists).

EDIT: I discovered SnappyDB today . It uses the same serialization library ( Kryo ) as paper, it seems to be very similar to paper with a lot of functions for key management.

So my question is the following:

Should I look for workarounds and keep using Realm, if so, what are the workarounds, or should I use Paper or SnappyDB instead? Has anyone used Paper or SnappyDB for Android

All the best

+3


source to share


2 answers


If you are wondering how to update your object on the UI thread when it changes in the background, it is actually quite simple.

For example, in your UI thread, you can do this:

private Dog dog;
private RealmChangeListener listener = new RealmChangeListener() {
    @Override
    // This will be called when the commitTransaction gets called 
    // in the background thread
    public void onChange() {
        // It would changed to "EFG" automatically in next UI loop after
        // you updated it in the background thread.
        String name = dog.getName();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    dog = realm.where(Dog.class).equalTo("id", 42).findFirst();
    // Assume it is "ABC" now
    String name = dog.getName();
    // Register the listener
    realm.addChangeListener(listener);
}

      

And update the dog in the background like:



// This realm will be a different instance created in this thread.
dog = realm.where(Dog.class).equalTo("id", 42).findFirst();
realm.beginTransaction();
dog.setName("EFG");
realm.commitTransaction();

      

IllegalStateException

occurs due to:

The only rule of thumb for using Realm in streaming is to remember that instances of Realm, RealmObject, or RealmResults cannot be streamed. If you want to access the same data from another thread, you should just get a new Realm instance (i.e. Realm.getInstance (context context) or its cousins) and get your objects via request. Objects will be mapped to the same data on disk and will be available for reading and writing from any stream! See See doc here

And you will probably need the RealmBaseAdapter , which can make creation ListView

with Realm pretty easy. You can find it here .

0


source


JPA is not a solution, this is the definition for Java Persistence. After choosing JPA, you need to find the implementation. In the Java world, the most widely used implementation is Hibernate. Moreover, you can use Hibernate ORM without using JPA.



On Android, OrmLite provides an implementation for a subset of JPA. But, since this is only a subset, you can also skip JPA and use the equivalent Ormlite annotations. I am using JPA implemented by Hibernate on my server-side apps and Ormlite without JPA on Android. I would definitely recommend Ormlite.

0


source







All Articles