Last active
October 22, 2018 19:54
-
-
Save LuiguiBalarezo/da2d29d0bc90256f2390196c21aedd2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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