Skip to content

Instantly share code, notes, and snippets.

@LuiguiBalarezo
Last active October 22, 2018 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuiguiBalarezo/da2d29d0bc90256f2390196c21aedd2d to your computer and use it in GitHub Desktop.
Save LuiguiBalarezo/da2d29d0bc90256f2390196c21aedd2d to your computer and use it in GitHub Desktop.
package com.zeta.scriptgo.zentinela.Utils;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import io.realm.Realm;
/**
* Created by lbalarezo on 09/03/2018.
*/
public class RealmManager {
private static String TAG = "RealmManager";
private static Realm realm;
//USADO UNA SOLA VEZ EN LA ACTIVIDAD PRINCIPAL
public static Realm open(String origin) {
setLog(origin, "Open Realm DB");
if (realm == null) {
setLog(origin, "Init Realm DB");
realm = Realm.getDefaultInstance();
}
return realm;
}
public static Realm using(String origin) {
setLog(origin, "Using Realm DB");
checkForOpenRealm(origin);
return realm;
}
//USADO UNA SOLA VEZ en "DESTROY"
public static void close(String origin) {
setLog(origin, "Close Realm DB");
if (realm != null) {
setLog(origin, "Init Close Realm DB");
realm.close();
}
}
public static void cancelTransaction(String origin) {
setLog(origin, "Cancel Transacion Realm DB");
if (realm != null && realm.isInTransaction()) {
setLog(origin, "Init CancelTransacion Realm DB");
realm.cancelTransaction();
}
}
private static void checkForOpenRealm(String origin) {
setLog(origin, "CheckForOpenRealm Realm DB");
if (realm == null || realm.isClosed()) {
setLog(origin, "ReOpen Realm DB");
open(origin);
}
}
private static void setLog(String origin, String status) {
Log.d(TAG, origin + " " + status);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment