Search in sources :

Example 1 with RealmObject

use of io.realm.RealmObject in project xabber-android by redsolution.

the class ChatManager method saveOrUpdateChatDataToRealm.

public void saveOrUpdateChatDataToRealm(final AbstractChat chat) {
    final long startTime = System.currentTimeMillis();
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            Realm realm = RealmManager.getInstance().getNewRealm();
            realm.executeTransaction(new Realm.Transaction() {

                @Override
                public void execute(Realm realm) {
                    String accountJid = chat.getAccount().toString();
                    String userJid = chat.getUser().toString();
                    ChatDataRealm chatRealm = realm.where(ChatDataRealm.class).equalTo("accountJid", accountJid).equalTo("userJid", userJid).findFirst();
                    if (chatRealm == null)
                        chatRealm = new ChatDataRealm(accountJid, userJid);
                    chatRealm.setLastPosition(chat.getLastPosition());
                    chatRealm.setArchived(chat.isArchived());
                    chatRealm.setHistoryRequestedAtStart(chat.isHistoryRequestedAtStart());
                    NotificationStateRealm notificationStateRealm = chatRealm.getNotificationState();
                    if (notificationStateRealm == null)
                        notificationStateRealm = new NotificationStateRealm();
                    notificationStateRealm.setMode(chat.getNotificationState().getMode());
                    notificationStateRealm.setTimestamp(chat.getNotificationState().getTimestamp());
                    chatRealm.setNotificationState(notificationStateRealm);
                    RealmObject realmObject = realm.copyToRealmOrUpdate(chatRealm);
                }
            });
        }
    });
    LogManager.d("REALM", Thread.currentThread().getName() + " save chat data: " + (System.currentTimeMillis() - startTime));
}
Also used : NotificationStateRealm(com.xabber.android.data.database.realm.NotificationStateRealm) RealmObject(io.realm.RealmObject) NotificationStateRealm(com.xabber.android.data.database.realm.NotificationStateRealm) Realm(io.realm.Realm) ChatDataRealm(com.xabber.android.data.database.realm.ChatDataRealm) ChatDataRealm(com.xabber.android.data.database.realm.ChatDataRealm)

Example 2 with RealmObject

use of io.realm.RealmObject in project T-MVP by north2016.

the class DbRealmAspect method aroundJoinPoint.

// 在连接点进行方法替换
@Around("methodAnnotated()")
public void aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
    // 执行原方法
    joinPoint.proceed();
    Realm realm = Realm.getDefaultInstance();
    Observable.fromArray(joinPoint.getArgs()).filter(new Predicate<Object>() {

        @Override
        public boolean test(Object obj) {
            return obj instanceof RealmObject || obj instanceof List;
        }
    }).subscribe(new Consumer<Object>() {

        @Override
        public void accept(Object obj) {
            realm.beginTransaction();
            if (obj instanceof List)
                realm.copyToRealmOrUpdate((List) obj);
            else
                realm.copyToRealmOrUpdate((RealmObject) obj);
            realm.commitTransaction();
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) {
            e.printStackTrace();
        }
    });
}
Also used : RealmObject(io.realm.RealmObject) RealmObject(io.realm.RealmObject) List(java.util.List) Realm(io.realm.Realm) Predicate(io.reactivex.functions.Predicate) Around(org.aspectj.lang.annotation.Around)

Aggregations

Realm (io.realm.Realm)2 RealmObject (io.realm.RealmObject)2 ChatDataRealm (com.xabber.android.data.database.realm.ChatDataRealm)1 NotificationStateRealm (com.xabber.android.data.database.realm.NotificationStateRealm)1 Predicate (io.reactivex.functions.Predicate)1 List (java.util.List)1 Around (org.aspectj.lang.annotation.Around)1