Skip to content

Instantly share code, notes, and snippets.

@magillus
Created June 7, 2017 21:57
Show Gist options
  • Save magillus/245403714ed21fad5a1e8c4a780bce57 to your computer and use it in GitHub Desktop.
Save magillus/245403714ed21fad5a1e8c4a780bce57 to your computer and use it in GitHub Desktop.
Realm Context wrapper.
package com.example.mat.rxjavaplayground.rxutil;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import io.reactivex.functions.Function;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmObject;
import timber.log.Timber;
/**
* Realm db wrapper for easier execution on same thread actions and transactions.
* Created on 4/6/17.
*/
public class RealmContext {
/**
* Realm configuration used for initializing realm.s
*/
protected RealmConfiguration realmConfiguration;
/**
* Create instance of RealmContext for db name and version.
*
* @param context
* @param name
* @param version
*/
public RealmContext(Context context, String name, int version, Object module) {
this(context, new RealmConfiguration.Builder()
.name(name)
.deleteRealmIfMigrationNeeded()
.schemaVersion(version)
.modules(module)
.build());
}
public RealmContext(Context context, RealmConfiguration configuration) {
Realm.init(context);
this.realmConfiguration = configuration;
}
/**
* Wraps function execution with Realm object that is released after function is run.
*
* @param function function that will use the Realm instance
* @param <R> return type from the function
* @return return value from the function
*/
public <R> R withRealm(Function<Realm, R> function) {
try (Realm realm = Realm.getInstance(realmConfiguration)) {
try {
return function.apply(realm);
} catch (Exception e) {
Timber.w(e, "Error calling function on Realm.");
}
}
return null;
}
/**
* Wraps function execution with Realm object that is released after function is run.
*
* @param function function that will use the Realm instance
* @param <R> return type from the function
* @return return value from the function
*/
public <R> R withRealmCopy(Function<Realm, R> function) {
try (Realm realm = Realm.getInstance(realmConfiguration)) {
try {
return copyFromRealm(realm, function.apply(realm));
} catch (Exception e) {
Timber.w(e, "Error calling function on Realm.");
}
}
return null;
}
public <R> R withTransaction(@Nullable Realm realm, Function<Realm, R> function) {
if (realm != null) {
boolean wasInTransaction = realm.isInTransaction();
try {
if (!wasInTransaction) {
realm.beginTransaction();
}
R retValue = function.apply(realm);
if (!wasInTransaction) {
realm.commitTransaction();
}
return retValue;
} catch (Exception ex) {
Timber.w(ex, "Error running realm with transaction.");
return null;
} finally {
// if all good should not be in transaction, cancel otherwise
if (!wasInTransaction && realm.isInTransaction()) {
realm.cancelTransaction();
}
}
} else {
return withRealm(newRealm -> withTransaction(newRealm, function));
}
}
/**
* Wraps function execution with Realm transaction and commits after function is run.
* It will commit transaction if function run without errors and cancel transaction if there were errors.
*
* @param function function that will use realm transaction.
* @param <R> return type from the function.
* @return return value from the function.
*/
public <R> R withTransaction(Function<Realm, R> function) {
return withTransaction(null, function);
}
/**
* Checks if null and copies from realm the object.
*
* @param realm
* @param object
* @param <R>
* @return
*/
@Nullable
public static <R> R copyFromRealm(@NonNull Realm realm, @Nullable R object) {
if (object != null) {
if (object instanceof RealmObject) {
return (R) realm.copyFromRealm((RealmObject) object);
} else {
return object;
}
}
return null;
}
/**
* Copies list of RealmObjects from Realm
* @param realm
* @param list
* @param <R>
* @return
*/
public static <R extends RealmObject> List<R> copyFromRealmList(@NonNull Realm realm, @NonNull List<R> list) {
if (list.isEmpty()) {
return Collections.emptyList();
} else {
return realm.copyFromRealm(list);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment