Guest User

Untitled

a guest
Dec 16th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. public class RealmManager {
  2. private static RealmManager instance;
  3. private final ThreadLocal<Realm> localRealm = new ThreadLocal<>();
  4.  
  5. RealmManager(){}
  6.  
  7. public synchronized static RealmManager getInstance(){
  8. if(instance == null){
  9. instance = new RealmManager();
  10. }
  11. return instance;
  12. }
  13.  
  14. public Realm openLocalInstance() {
  15. Realm realm = Realm.getDefaultInstance();
  16. if(localRealm.get() == null) {
  17. localRealm.set(realm);
  18. }
  19. return realm;
  20. }
  21.  
  22. public Realm getLocalInstance() {
  23. Realm realm = localRealm.get();
  24. if(realm == null) {
  25. throw new IllegalStateException("No open Realms were found on this thread.");
  26. }
  27. return realm;
  28. }
  29.  
  30. public void closeLocalInstance() {
  31. Realm realm = localRealm.get();
  32. if(realm == null) {
  33. throw new IllegalStateException(
  34. "Cannot close a Realm that is not open.");
  35. }
  36. realm.close();
  37. if(Realm.getLocalInstanceCount(Realm.getDefaultConfiguration()) <= 0) {
  38. localRealm.set(null);
  39. }
  40. }
  41.  
  42. public void storePreferenceDao(int userID, String rank){
  43. final PreferenceDao preferenceDao = new PreferenceDao();
  44. preferenceDao.setUserID(userID);
  45. preferenceDao.setRank(rank);
  46. openLocalInstance();
  47. getLocalInstance().executeTransactionAsync(new Realm.Transaction() {
  48. @Override
  49. public void execute(Realm realm) {
  50. realm.copyToRealmOrUpdate(preferenceDao);
  51. }
  52. }, new Realm.Transaction.OnSuccess(){
  53. @Override
  54. public void onSuccess(){
  55. System.out.println("Data is stored successfully!");
  56. }
  57. }, new Realm.Transaction.OnError(){
  58. @Override
  59. public void onError(Throwable error){
  60. System.out.println("There is an error in storePreferenceDao()");
  61. }
  62. });
  63. closeLocalInstance();
  64. }
  65.  
  66. RealmManager.getInstance().storePreferenceDao(123, "Alpaca");
  67.  
  68. public class MySingleton {
  69. private static MySingleton sMySingleton;
  70.  
  71. //private constructor.
  72. private MySingleton() {
  73. if (sMySingleton != null){
  74. throw new RuntimeException("Use getInstance() for the instance");
  75. }
  76. }
  77.  
  78. public synchronized static MySingleton getInstance() {
  79. if (sMySingleton == null){
  80. sMySingleton = new MySingleton();
  81. }
  82. return sMySingleton;
  83. }
  84. }
  85.  
  86. public class BaseActivity extends AppCompatActivity {
  87. public Realm realm;
  88. @Override
  89. protected void onCreate(Bundle savedInstanceState) {
  90. super.onCreate(savedInstanceState);
  91. setContentView(R.layout.activity_base);
  92. realm = Realm.getDefaultInstance();
  93. }
  94.  
  95. }
  96.  
  97. public class MainActivity extends BaseActivity {
  98.  
  99. @Override
  100. protected void onCreate(Bundle savedInstanceState) {
  101. super.onCreate(savedInstanceState);
  102. setContentView(R.layout.activity_main);
  103. //Here you can directly access realm object and perform your task
  104. realm.where()//just example
  105. }
  106. @Override
  107. protected void onDestroy() {
  108. super.onDestroy();
  109. if(realm!=null)
  110. realm.close();
  111. //Don't forget to close realm object
  112. }
  113.  
  114. }
Add Comment
Please, Sign In to add comment