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

The following examples show how to use io.realm.Realm#cancelTransaction() . 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: MemberRealm.java    From talk-android with MIT License 6 votes vote down vote up
public void removeQuitMemberOnMainThread() {
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        RealmResults<Member> realmResults = realm.where(Member.class)
                .equalTo(Member.TEAM_ID, BizLogic.getTeamId())
                .equalTo(Member.IS_QUIT, true)
                .findAll();
        for (Member realmResult : realmResults) {
            if (realmResult != null) {
                realmResult.removeFromRealm();
            }
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 2
Source File: MemberRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Member> getMembersByIdsWithCurrentThread(final List<String> ids) {
    List<Member> members = new ArrayList<>(ids.size());
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        for (String id : ids) {
            Member member = new Member();
            Member realmMember = realm.where(Member.class)
                    .equalTo(Member.TEAM_ID, BizLogic.getTeamId())
                    .equalTo(Member.ID, id).findFirst();
            if (realmMember == null) continue;
            copy(member, realmMember);
            members.add(member);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return members;
}
 
Example 3
Source File: MessageRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Message> getLocalUnreadMessagesWithCurrentThread(final String foreignId) {
    List<Message> messages = new ArrayList<>();
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        RealmResults<Message> realmResults = realm.where(Message.class)
                .equalTo(Message.TEAM_ID, BizLogic.getTeamId())
                .equalTo(Message.FOREIGN_ID, foreignId)
                .equalTo(Message.IS_READ, false)
                .findAll();
        realmResults.sort(Message.CREATE_AT_TIME, Sort.ASCENDING);
        for (Message realmResult : realmResults) {
            Message message = new Message();
            copy(message, realmResult);
            messages.add(message);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return messages;
}
 
Example 4
Source File: MemberRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Member> getNotQuitAndNotRobotMemberWithCurrentThread() {
    final List<Member> members = new ArrayList<>();
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        RealmResults<Member> realmResults = realm.where(Member.class)
                .equalTo(Member.TEAM_ID, BizLogic.getTeamId())
                .equalTo(Member.IS_QUIT, false)
                .equalTo(Member.IS_ROBOT, false)
                .findAll();
        realmResults.sort(Member.ALIAS_PINYIN, Sort.ASCENDING);
        for (Member realmResult : realmResults) {
            Member member = new Member();
            copy(member, realmResult);
            members.add(member);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return members;
}
 
Example 5
Source File: MessageRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Message> getUnSendMessageWithCurrentThread(final String foreignId) {
    final List<Message> messages = new ArrayList<>();
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        RealmResults<Message> realmResults = realm.where(Message.class)
                .equalTo(Message.TEAM_ID, BizLogic.getTeamId())
                .equalTo(Message.FOREIGN_ID, foreignId)
                .notEqualTo(Message.STATUS, 0)
                .findAll();
        realmResults.sort(Message.CREATE_AT_TIME, Sort.ASCENDING);
        for (Message realmResult : realmResults) {
            final Message message = new Message();
            copy(message, realmResult);
            messages.add(message);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return messages;
}
 
Example 6
Source File: MemberRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Member> getQuitMemberWithCurrentThread() {
    final List<Member> members = new ArrayList<>();
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        RealmResults<Member> realmResults = realm.where(Member.class)
                .equalTo(Member.TEAM_ID, BizLogic.getTeamId())
                .equalTo(Member.IS_QUIT, true)
                .findAll();
        realmResults.sort(Member.ALIAS_PINYIN, Sort.ASCENDING);
        for (Member realmResult : realmResults) {
            Member member = new Member();
            copy(member, realmResult);
            members.add(member);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return members;
}
 
Example 7
Source File: RealmUtil.java    From photosearcher with Apache License 2.0 6 votes vote down vote up
public static void executeTransaction(Action1<Realm> transaction, Action1<Throwable> onFailure) {
    Realm realm = null;

    try {
        realm = Realm.getDefaultInstance();
        realm.beginTransaction();

        transaction.call(realm);

        realm.commitTransaction();
    } catch (Exception e) {
        if (realm != null) {
            realm.cancelTransaction();
        }
        onFailure.call(e);
    } finally {
        if (realm != null) {
            realm.close();
        }
    }
}
 
Example 8
Source File: MemberRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Member> getMemberWithCurrentThread() {
    final List<Member> members = new ArrayList<>();
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        RealmResults<Member> realmResults = realm.where(Member.class)
                .equalTo(Member.TEAM_ID, BizLogic.getTeamId())
                .equalTo(Member.IS_QUIT, false)
                .findAll();
        realmResults.sort(Member.ALIAS_PINYIN, Sort.ASCENDING);
        for (Member realmResult : realmResults) {
            Member member = new Member();
            copy(member, realmResult);
            members.add(member);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return members;
}
 
Example 9
Source File: RoomRealm.java    From talk-android with MIT License 6 votes vote down vote up
public void leaveWithCurrentThread(final Room room, final String memberId) {
    if (room == null) return;
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        if (BizLogic.isMe(memberId)) {
            Room realmRoom = realm.where(Room.class)
                    .equalTo(Room.TEAM_ID, BizLogic.getTeamId())
                    .equalTo(Room.ID, room.get_id())
                    .findFirst();
            realmRoom.setIsArchived(true);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 10
Source File: NotificationRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Notification> getAllNotificationWithCurrentThread() {
    List<Notification> notifications = new ArrayList<>();
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        RealmResults<Notification> realmResults = realm
                .where(Notification.class)
                .equalTo(Notification.TEAM_ID, BizLogic.getTeamId())
                .findAll();
        for (Notification realmResult : realmResults) {
            Notification notification = new Notification();
            copy(notification, realmResult);
            copyObject(notification, realmResult);
            notifications.add(notification);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return notifications;
}
 
Example 11
Source File: RoomRealm.java    From talk-android with MIT License 6 votes vote down vote up
public void batchAddWithCurrentThread(final List<Room> rooms) {
    final List<Room> realmRooms = new ArrayList<>(rooms.size());
    Realm realm = RealmProvider.getInstance();
    try {
        for (Room room : rooms) {
            Room realmRoom = new Room();
            copy(realmRoom, room);
            realmRooms.add(realmRoom);
        }
        realm.beginTransaction();
        realm.copyToRealmOrUpdate(realmRooms);
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 12
Source File: RealmUtils.java    From quill with MIT License 6 votes vote down vote up
public static <T> T executeTransaction(@NonNull Realm realm,
                                       @NonNull RealmTransactionWithReturn<T> transaction) {
    T retValue;
    realm.beginTransaction();
    try {
        retValue = transaction.execute(realm);
        realm.commitTransaction();
    } catch (Throwable e) {
        if (realm.isInTransaction()) {
            realm.cancelTransaction();
        } else {
            Log.w(TAG, "Could not cancel transaction, not currently in a transaction.");
        }
        throw e;
    }
    return retValue;
}
 
Example 13
Source File: MemberRealm.java    From talk-android with MIT License 6 votes vote down vote up
public void batchAddWithCurrentThread(final List<Member> members) {
    final List<Member> realmMembers = new ArrayList<>(members.size());
    Realm memberRealm = RealmProvider.getInstance();
    for (Member member : members) {
        Member realmMember = new Member();
        copy(realmMember, member);
        realmMembers.add(realmMember);
    }
    try {
        memberRealm.beginTransaction();
        memberRealm.copyToRealmOrUpdate(realmMembers);
        memberRealm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        memberRealm.cancelTransaction();
    } finally {
        memberRealm.close();
    }
}
 
Example 14
Source File: RoomRealm.java    From talk-android with MIT License 6 votes vote down vote up
public Room getRoomWithCurrentThread(final String roomId) {
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        Room realmRoom = realm.where(Room.class)
                .equalTo(Room.TEAM_ID, BizLogic.getTeamId())
                .equalTo(Room.ID, roomId)
                .findFirst();
        if (realmRoom == null) return null;
        Room room = new Room();
        copy(room, realmRoom);
        realm.commitTransaction();
        return room;
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return null;
}
 
Example 15
Source File: StoryRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Story> getAllStoryWithCurrentThread() {
    List<Story> stories = new ArrayList<>();
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        RealmResults<Story> realmResults = realm.where(Story.class)
                .equalTo(Story.TEAM_ID, BizLogic.getTeamId())
                .findAll();
        for (Story realmResult : realmResults) {
            Story story = new Story();
            copy(story, realmResult);
            stories.add(story);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return stories;
}
 
Example 16
Source File: NotificationRealm.java    From talk-android with MIT License 5 votes vote down vote up
public void addWithCurrentThread(final Notification notification) {
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        Notification realmNotification = realm.createObject(Notification.class);
        copy(realmNotification, notification);
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 17
Source File: TokensRealmSource.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Override
public void setEnable(NetworkInfo network, Wallet wallet, Token token, boolean isEnabled) {
    Realm realm = null;
    try {
        token.tokenInfo.isEnabled = isEnabled;
        realm = realmManager.getRealmInstance(wallet);
        RealmToken realmToken = realm.where(RealmToken.class)
                .equalTo("address", databaseKey(token))
                .equalTo("chainId", token.tokenInfo.chainId)
                .findFirst();

        TransactionsRealmCache.addRealm();
        realm.beginTransaction();
        if (realmToken != null) {
            realmToken.setEnabled(isEnabled);
        }
        realm.commitTransaction();
    } catch (Exception ex) {
        if (realm != null && realm.isInTransaction()) {
            realm.cancelTransaction();
        }
    } finally {
        if (realm != null) {
            realm.close();
            TransactionsRealmCache.subRealm();
        }
    }
}
 
Example 18
Source File: TeamRealm.java    From talk-android with MIT License 5 votes vote down vote up
public void addWithCurrentThread(final Team team) {
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        Team teamInfo = realm.createObject(Team.class);
        copy(teamInfo, team);
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 19
Source File: MemberRealm.java    From talk-android with MIT License 5 votes vote down vote up
public List<Member> getAdminsMemberWithCurrentThread() {
    final List<Member> members = new ArrayList<>();
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        final RealmResults<Member> realmResults = realm.where(Member.class)
                .equalTo(Member.TEAM_ID, BizLogic.getTeamId())
                .beginGroup()
                .equalTo(Member.ROLE, Member.ADMIN)
                .or()
                .equalTo(Member.ROLE, Member.OWNER)
                .endGroup()
                .findAll();
        realmResults.sort(Member.ALIAS_PINYIN, Sort.ASCENDING);
        for (Member realmMember : realmResults) {
            Member member = new Member();
            copy(member, realmMember);
            members.add(member);
        }
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
    return members;
}
 
Example 20
Source File: MessageRealm.java    From talk-android with MIT License 5 votes vote down vote up
public void addOrUpdateEventWithCurrentThread(Message message, final String... msgId) {
    final Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        copy(message, message);
        realm.copyToRealmOrUpdate(message);
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}