Java Code Examples for io.realm.Realm#executeTransaction()

The following examples show how to use io.realm.Realm#executeTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RealmTokenSource.java    From Upchain-wallet with GNU Affero General Public License v3.0 6 votes vote down vote up
private void putInNeed(NetworkInfo networkInfo, String walletAddress, TokenInfo tokenInfo) {
    Realm realm = null;
    try {
        realm = getRealmInstance(networkInfo, walletAddress);
        RealmTokenInfo realmTokenInfo = realm.where(RealmTokenInfo.class)
                .equalTo("address", tokenInfo.address)
                .findFirst();
        if (realmTokenInfo == null) {
            realm.executeTransaction(r -> {
                RealmTokenInfo obj = r.createObject(RealmTokenInfo.class, tokenInfo.address);
                obj.setName(tokenInfo.name);
                obj.setSymbol(tokenInfo.symbol);
                obj.setDecimals(tokenInfo.decimals);
                obj.setAddedTime(System.currentTimeMillis());
            });
        }
    } finally {
        if (realm != null) {
            realm.close();
        }
    }
}
 
Example 2
Source File: RealmTokenSource.java    From ETHWallet with GNU General Public License v3.0 6 votes vote down vote up
private void putInNeed(NetworkInfo networkInfo, Wallet wallet, TokenInfo tokenInfo) {
    Realm realm = null;
    try {
        realm = getRealmInstance(networkInfo, wallet);
        RealmTokenInfo realmTokenInfo = realm.where(RealmTokenInfo.class)
                .equalTo("address", tokenInfo.address)
                .findFirst();
        if (realmTokenInfo == null) {
            realm.executeTransaction(r -> {
                RealmTokenInfo obj = r.createObject(RealmTokenInfo.class, tokenInfo.address);
                obj.setName(tokenInfo.name);
                obj.setSymbol(tokenInfo.symbol);
                obj.setDecimals(tokenInfo.decimals);
                obj.setAddedTime(System.currentTimeMillis());
            });
        }
    } finally {
        if (realm != null) {
            realm.close();
        }
    }
}
 
Example 3
Source File: RealmTokenSource.java    From trust-wallet-android-source with GNU General Public License v3.0 6 votes vote down vote up
private void putInNeed(NetworkInfo networkInfo, Wallet wallet, TokenInfo tokenInfo) {
    Realm realm = null;
    try {
        realm = getRealmInstance(networkInfo, wallet);
        RealmTokenInfo realmTokenInfo = realm.where(RealmTokenInfo.class)
                .equalTo("address", tokenInfo.address)
                .findFirst();
        if (realmTokenInfo == null) {
            realm.executeTransaction(r -> {
                RealmTokenInfo obj = r.createObject(RealmTokenInfo.class, tokenInfo.address);
                obj.setName(tokenInfo.name);
                obj.setSymbol(tokenInfo.symbol);
                obj.setDecimals(tokenInfo.decimals);
                obj.setAddedTime(System.currentTimeMillis());
            });
        }
    } finally {
        if (realm != null) {
            realm.close();
        }
    }
}
 
Example 4
Source File: Stats.java    From 600SeriesAndroidUploader with MIT License 6 votes vote down vote up
private void writeRecords() {
    if (loadedRecords != null && loadedRecords.size() > 0) {
        final Realm storeRealm = Realm.getInstance(UploaderApplication.getStoreConfiguration());

        try {

            storeRealm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(@NonNull Realm realm) {
                    for (LoadedRecord loadedRecord : loadedRecords) {
                        if (loadedRecord.isWrite) {
                            if (open < 1) loadedRecord.isWrite = false;
                            storeRealm.copyToRealmOrUpdate(loadedRecord.statRecord);
                            Log.d(TAG, String.format("write: %s key: %s", loadedRecord.statRecord.getClass().getSimpleName(), loadedRecord.statRecord.getKey()));
                        }
                    }
                }
            });

        } catch (Exception e) {
            Log.w(TAG, "Stats write records, Realm task could not complete");
        }

        storeRealm.close();
    }
}
 
Example 5
Source File: MasterService.java    From 600SeriesAndroidUploader with MIT License 6 votes vote down vote up
private void cnlReady() {
    Realm storeRealm = Realm.getInstance(UploaderApplication.getStoreConfiguration());
    storeRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(@NonNull Realm realm) {
            DataStore dataStore = realm.where(DataStore.class).findFirst();
            dataStore.clearAllCommsErrors();

            long plug = System.currentTimeMillis();
            dataStore.setCnlPlugTimestamp(plug);
            long unplug = dataStore.getCnlUnplugTimestamp();
            if (unplug == 0) unplug = plug;
            if (plug - unplug > dataStore.getPushoverBackfillLimiter() * 60000L)
                dataStore.setCnlLimiterTimestamp(plug);
        }
    });
    storeRealm.close();

    StatCnl statCnl = ((StatCnl) Stats.open().readRecord(StatCnl.class));
    statCnl.connected();
    UserLogMessage.sendE(mContext, UserLogMessage.TYPE.NOTE, statCnl.toString());
    Stats.close();
}
 
Example 6
Source File: MasterService.java    From 600SeriesAndroidUploader with MIT License 6 votes vote down vote up
private void usbDetached() {
    showDisconnectionNotification(FormatKit.getInstance().getString(R.string.ul_usb__usb_error),
            FormatKit.getInstance().getString(R.string.ul_usb__contour_next_link_unplugged));
    UserLogMessage.send(mContext, UserLogMessage.TYPE.WARN, String.format("{id;%s}. {id;%s}",
            R.string.ul_usb__usb_error,
            R.string.ul_usb__contour_next_link_unplugged));

    statusNotification.updateNotification(StatusNotification.NOTIFICATION.ERROR);

    ((StatCnl) Stats.open().readRecord(StatCnl.class)).disconnected();
    Stats.close();

    Realm storeRealm = Realm.getInstance(UploaderApplication.getStoreConfiguration());
    storeRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(@NonNull Realm realm) {
            DataStore dataStore = realm.where(DataStore.class).findFirst();
            long now = System.currentTimeMillis();
            // allow for jitter, only change timestamp when >period since last plug
            if (dataStore.getCnlUnplugTimestamp() == 0
                    || now - dataStore.getCnlPlugTimestamp() > CNL_JITTER_MS)
                dataStore.setCnlUnplugTimestamp(now);
        }
    });
    storeRealm.close();
}
 
Example 7
Source File: DatabaseHelper.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
public static void deleteUser(Realm realm, User user, Realm.Transaction.Callback callback){
    if(callback != null){
        realm.executeTransaction(realmRef -> deleteUser(user), callback);
    }else{
        realm.executeTransaction(realmRef -> deleteUser(user));
    }
}
 
Example 8
Source File: CharacterLocalDataSource.java    From Game-of-Thrones with Apache License 2.0 5 votes vote down vote up
private void remove(GoTCharacter character) {
  Realm realm = Realm.getDefaultInstance();
  realm.executeTransaction(realm1 -> {
    BddGoTCharacter bddGoTCharacter = find(character);
    if (bddGoTCharacter != null) {
      bddGoTCharacter.deleteFromRealm();
    }
  });
  realm.close();
}
 
Example 9
Source File: CharacterLocalDataSource.java    From Game-of-Thrones with Apache License 2.0 5 votes vote down vote up
private void save(BddGoTCharacter character) {
  Realm realm = Realm.getDefaultInstance();
  realm.executeTransaction(realm1 -> {
    realm1.copyToRealmOrUpdate(character);
  });
  realm.close();
}
 
Example 10
Source File: HouseLocalDataSource.java    From Game-of-Thrones with Apache License 2.0 5 votes vote down vote up
private void remove(GoTHouse house) {
  Realm realm = Realm.getDefaultInstance();
  realm.executeTransaction(realm1 -> {
    BddHouse bddHouse = find(house);
    if (bddHouse != null) {
      bddHouse.deleteFromRealm();
    }
  });
  realm.close();
}
 
Example 11
Source File: HouseLocalDataSource.java    From Game-of-Thrones with Apache License 2.0 5 votes vote down vote up
private void save(BddHouse house) {
  Realm realm = Realm.getDefaultInstance();
  realm.executeTransaction(realm1 -> {
    realm1.copyToRealmOrUpdate(house);
  });
  realm.close();
}
 
Example 12
Source File: ForumUsersCache.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
public static void saveUsers(List<ForumUser> forumUsers) {
    Realm realmInstance = Realm.getDefaultInstance();
    realmInstance.executeTransaction(realm -> {
        List<ForumUserBd> bdList = new ArrayList<>();
        for (ForumUser item : forumUsers) {
            bdList.add(new ForumUserBd(item));
        }
        realm.insertOrUpdate(bdList);
    });
    realmInstance.close();
}
 
Example 13
Source File: UserLogMessage.java    From 600SeriesAndroidUploader with MIT License 5 votes vote down vote up
public void stale() {
    final Realm userLogRealm = Realm.getInstance(UploaderApplication.getUserLogConfiguration());

    try {
        userLogRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(@NonNull Realm realm) {
                // remove stale items
                RealmResults results = realm.where(UserLog.class)
                        .findAll();
                if (results.size() > MESSAGES_MAX) {
                    int count = results.size() - MESSAGES_MAX;
                    results.where()
                            .sort("index", Sort.ASCENDING)
                            .limit(count)
                            .findAll()
                            .deleteAllFromRealm();
                    Log.d(TAG, String.format("removed %s stale items", count));
                }
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "Could not remove stale messages: " + e.getMessage());
    }

    userLogRealm.close();
}
 
Example 14
Source File: RealmUserInfo.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void updateEmail(final String email) {
    Realm realm1 = Realm.getDefaultInstance();
    realm1.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            RealmUserInfo realmUserInfo = realm.where(RealmUserInfo.class).findFirst();
            if (realmUserInfo != null) {
                realmUserInfo.setEmail(email);
            }
        }
    });
    realm1.close();
}
 
Example 15
Source File: RealmUserInfo.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void updateSelfRemove(final int selfRemove) {
    Realm realm1 = Realm.getDefaultInstance();
    realm1.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            RealmUserInfo realmUserInfo = realm.where(RealmUserInfo.class).findFirst();
            if (realmUserInfo != null) {
                realmUserInfo.setSelfRemove(selfRemove);
            }
        }
    });
    realm1.close();
}
 
Example 16
Source File: RealmRoom.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void updateChatTitle(final long userId, final String title) {// TODO [Saeed Mozaffari] [2017-10-24 3:36 PM] - Can Write Better Code?
    Realm realm1 = Realm.getDefaultInstance();
    realm1.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            for (RealmRoom realmRoom : realm.where(RealmRoom.class).equalTo(RealmRoomFields.TYPE, ProtoGlobal.Room.Type.CHAT.toString()).findAll()) {
                if (realmRoom.getChatRoom() != null && realmRoom.getChatRoom().getPeerId() == userId) {
                    realmRoom.setTitle(title.trim());
                }
            }
        }
    });
    realm1.close();
}
 
Example 17
Source File: LinksPresenterImpl.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void enableNsfwPreview() {
    Realm realm = linksView.getContentContext().getBaseActivity().getRealm();
    if(realm != null){
        realm.executeTransaction(instance -> getPrefs().setDisableNsfwPreview(false));
    }
}
 
Example 18
Source File: RealmManagerBase.java    From exchange-rates-mvvm with Apache License 2.0 5 votes vote down vote up
protected void executeInTransaction(Realm.Transaction callable) {
    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        realm.executeTransaction(callable);
    } finally {
        if (realm != null) {
            realm.close();
        }
    }
}
 
Example 19
Source File: BookmarkUtils.java    From carstream-android-auto with Apache License 2.0 5 votes vote down vote up
public static void deleteBookmark(final Bookmark bookmark) {
    Realm realm = Realm.getDefaultInstance();
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm1) {
            bookmark.deleteFromRealm();
        }
    });
}
 
Example 20
Source File: MasterService.java    From 600SeriesAndroidUploader with MIT License 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "onCreate called");

    mContext = this.getBaseContext();

    final Realm storeRealm = Realm.getInstance(UploaderApplication.getStoreConfiguration());
    final DataStore dataStore = storeRealm.where(DataStore.class).findFirst();

    // safety check: don't proceed until main ui has been run and datastore initialised
    if (dataStore != null) {

        storeRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(@NonNull Realm realm) {
                dataStore.clearAllCommsErrors();
                // check Xdrip available
                dataStore.setXdripPlusUploadAvailable(false);
                // check Nightscout site available
                dataStore.setNightscoutReportTime(0);
                dataStore.setNightscoutAvailable(false);
                // revalidate Pushover account
                dataStore.setPushoverAPItokenCheck("");
                dataStore.setPushoverUSERtokenCheck("");
            }
        });

        masterServiceReceiver = new MasterServiceReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.ACTION_CNL_COMMS_ACTIVE);
        intentFilter.addAction(Constants.ACTION_CNL_COMMS_FINISHED);
        intentFilter.addAction(Constants.ACTION_CNL_COMMS_READY);
        intentFilter.addAction(Constants.ACTION_STOP_SERVICE);
        intentFilter.addAction(Constants.ACTION_READ_NOW);
        intentFilter.addAction(Constants.ACTION_READ_PROFILE);
        intentFilter.addAction(Constants.ACTION_READ_OVERDUE);
        intentFilter.addAction(Constants.ACTION_SETTINGS_CHANGED);
        intentFilter.addAction(Constants.ACTION_URCHIN_UPDATE);
        intentFilter.addAction(Constants.ACTION_STATUS_UPDATE);
        intentFilter.addAction(Constants.ACTION_HEARTBEAT);
        intentFilter.addAction(Constants.ACTION_USERLOG_MESSAGE);

        intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
        intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
        intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);

        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        intentFilter.addAction(Constants.ACTION_USB_ACTIVITY);
        intentFilter.addAction(Constants.ACTION_USB_PERMISSION);
        intentFilter.addAction(Constants.ACTION_NO_USB_PERMISSION);

        intentFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
        intentFilter.addAction(Intent.ACTION_DATE_CHANGED);

        registerReceiver(masterServiceReceiver, intentFilter);

        statusNotification = new StatusNotification();

        serviceStart = true;

    } else {

        serviceStart = false;
    }

    storeRealm.close();
}