Zhuinden / realm-book-example

This is an example rewrite of AndroidHive's messy tutorial, accompanying the following article on Realm.
https://medium.com/@Zhuinden/how-to-use-realm-for-android-like-a-champ-and-how-to-tell-if-youre-doing-it-wrong-ac4f66b7f149
Other
80 stars 19 forks source link

Add example for background operation and synchronous transaction #3

Closed Zhuinden closed 7 years ago

Zhuinden commented 8 years ago

The example managed to end up using only realm.executeTransactionAsync().

On background threads, you're supposed to use the following pattern:

public Void doInBackground(Void... nothing) {
    // start of background thread
    Realm realm = null;
    try {
         realm = Realm.getDefaultInstance();
         realm.executeTransaction(new Realm.Transaction() {
              @Override
              public void execute(Realm realm) {
                  // do execute
              }
         });
    } finally {
        if(realm != null) {
             realm.close();
        }
    }
    // end of background thread
}

This example is missing from the current sample code.

Zhuinden commented 7 years ago

Background operations are shown in https://github.com/Zhuinden/xkcd-example/ (DownloadTask)