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

The following examples show how to use io.realm.Realm#commitTransaction() . 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 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 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: StoryRealm.java    From talk-android with MIT License 6 votes vote down vote up
public void batchAddWithCurrentThread(final List<Story> stories) {
    Realm realm = RealmProvider.getInstance();
    try {
        final List<Story> realmStories = new ArrayList<>(stories.size());
        for (Story story : stories) {
            Story realmStory = new Story();
            copy(realmStory, story);
            realmStories.add(realmStory);
        }
        realm.beginTransaction();
        realm.copyToRealmOrUpdate(realmStories);
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 4
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 5
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 6
Source File: ReadLaterArticlesLocalSource.java    From WanAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void insertReadLaterArticle(int userId, int id, long timeStamp) {
    //Build a new instance of ReadLaterArticleData by the article searched by its id in database of ArticleDetailData
    // then insert the instance to the database of ReadLaterArticleData
    Realm realm = RealmHelper.newRealmInstance();
    ArticleDetailData articleDetailData = realm.copyFromRealm(
            realm.where(ArticleDetailData.class)
                    .equalTo("id", id)
                    .findFirst()
    );
    ReadLaterArticleData data = new ReadLaterArticleData();
    data.setId(id);
    data.setTitle(articleDetailData.getTitle());
    data.setLink(articleDetailData.getLink());
    data.setAuthor(articleDetailData.getAuthor());
    data.setChapterId(articleDetailData.getChapterId());
    data.setChapterName(articleDetailData.getChapterName());
    data.setUserId(userId);
    data.setTimestamp(timeStamp);
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(data);
    realm.commitTransaction();
    realm.close();

}
 
Example 7
Source File: ImageImproveService.java    From GankApp with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 预解码图片并将抓到的图片尺寸保存至数据库
 *
 * @param realm   Realm 实例
 * @param image 图片
 * @return 是否保存成功
 */
private boolean saveToDb(Realm realm, Image image) {
    realm.beginTransaction();
    try {
        Point size = new Point();
        loadImageForSize(image.getUrl(),size);
        image.setHeight(size.y);
        image.setWidth(size.x);
        realm.copyToRealmOrUpdate(image);
    } catch (IOException e) {
        Logger.d("Failed to fetch image");
        realm.cancelTransaction();
        return false;
    }
    realm.commitTransaction();
    return true;
}
 
Example 8
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 9
Source File: MemberRealm.java    From talk-android with MIT License 6 votes vote down vote up
public List<Member> getMemberNotAdminWithCurrentThread() {
    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())
                .equalTo(Member.IS_QUIT, false)
                .equalTo(Member.ROLE, Member.MEMBER)
                .findAllSorted(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 10
Source File: DatabaseRealm.java    From openwebnet-android with MIT License 5 votes vote down vote up
public <T extends RealmObject> Iterable<T> addAll(Iterable<T> models) {
    Realm realm = getRealmInstance();
    realm.beginTransaction();
    realm.copyToRealm(models);
    realm.commitTransaction();
    return models;
}
 
Example 11
Source File: xDripIncoming.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static void New_data(Intent intent, Realm realm){
    Log.d(TAG, "New xDrip Broadcast Received");

    if (intent == null) return;
    Bundle bundle = intent.getExtras();
    if (bundle == null) return;

    final double bgEstimate = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE,0);
    if (bgEstimate == 0) return;

    final Bg bg = new Bg();
    bg.setDirection     (bundle.getString(Intents.EXTRA_BG_SLOPE_NAME));
    bg.setBattery       (bundle.getInt(Intents.EXTRA_SENSOR_BATTERY));
    bg.setBgdelta       (bundle.getDouble(Intents.EXTRA_BG_SLOPE, 0) * 1000 * 60 * 5);
    bg.setDatetime      (new Date(bundle.getLong(Intents.EXTRA_TIMESTAMP, new Date().getTime())));
    bg.setSgv           (Integer.toString((int) bgEstimate, 10));

    realm.beginTransaction();
    realm.copyToRealm(bg);
    realm.commitTransaction();

    Log.d(TAG, "New BG saved, sending out UI Update");

    Intent updateIntent = new Intent(Intents.UI_UPDATE);
    updateIntent.putExtra("UPDATE", "NEW_BG");
    LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(updateIntent);
}
 
Example 12
Source File: Database.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public <E extends RealmObject> void deleteAll(Class<E> clazz) {
  Realm realm = get();
  try {
    realm.beginTransaction();
    realm.delete(clazz);
    realm.commitTransaction();
  } finally {
    if (realm != null) {
      realm.close();
    }
  }
}
 
Example 13
Source File: MemberRealm.java    From talk-android with MIT License 5 votes vote down vote up
public void addOrUpdateWithCurrentThread(final Member member) {
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        copy(member, member);
        realm.copyToRealmOrUpdate(member);
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 14
Source File: TeamRealm.java    From talk-android with MIT License 5 votes vote down vote up
public void batchAddWithCurrentThread(final List<Team> teams) {
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        final String json = gson.toJson(teams);
        realm.createOrUpdateAllFromJson(Team.class, json);
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 15
Source File: ArticlesDataRemoteSource.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
private void saveToRealm(@NonNull ArticleDetailData article) {
    // It is necessary to build a new realm instance
    // in a different thread.
    Realm realm = Realm.getInstance(new RealmConfiguration.Builder()
            .name(RealmHelper.DATABASE_NAME)
            .deleteRealmIfMigrationNeeded()
            .build());
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(article);
    realm.commitTransaction();
    realm.close();
}
 
Example 16
Source File: RoomsListManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public void setShowChat(Realm realm, Chat mChat) {
    realm.beginTransaction();
    mChat.setShow(true);
    realm.copyToRealmOrUpdate(mChat);
    realm.commitTransaction();
    realm.close();
}
 
Example 17
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 18
Source File: pumpAction.java    From HAPP with GNU General Public License v3.0 5 votes vote down vote up
public static void setTempBasal(TempBasal newTempBasal, Realm realm){
    APSResult apsResult = APSResult.last(realm);
    realm.beginTransaction();
    apsResult.setAccepted(true);
    realm.commitTransaction();

    if (newTempBasal == null) newTempBasal = apsResult.getBasal();

    if (newTempBasal.checkIsCancelRequest()){
        cancelTempBasal(realm);

    } else {

        Profile p = new Profile(new Date());
        Safety safety = new Safety();

        //Sanity check the suggested rate is safe
        if (!safety.checkIsSafeMaxBolus(newTempBasal.getRate())) newTempBasal.setRate(safety.getMaxBasal(p));

        //Save
        newTempBasal.setStart_time(new Date());
        realm.beginTransaction();
        realm.copyToRealm(newTempBasal);
        realm.commitTransaction();

        //Clear notifications
        Notifications.clear("updateCard");
        Notifications.clear("newTemp");

        //Inform Integrations Manager
        IntegrationsManager.newTempBasal(newTempBasal, realm);

        //Update UI
        Intent intentUpdate = new Intent(Intents.UI_UPDATE);
        intentUpdate.putExtra("UPDATE", "NEW_APS_RESULT");
        LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intentUpdate);

    }
}
 
Example 19
Source File: DatabaseRealm.java    From openwebnet-android with MIT License 5 votes vote down vote up
public <T extends RealmObject> void delete(Class<T> clazz, String field, Integer value) {
    Realm realm = getRealmInstance();
    RealmResults<T> models = realm.where(clazz).equalTo(field, value).findAll();
    realm.beginTransaction();
    models.deleteAllFromRealm();
    realm.commitTransaction();
}
 
Example 20
Source File: RoomManager.java    From mangosta-android with Apache License 2.0 4 votes vote down vote up
public void loadMUCLightRooms() {

        final XMPPTCPConnection connection = XMPPSession.getInstance().getXMPPConnection();

        if (connection.isAuthenticated()) {

            DiscoverItems discoverItems = XMPPSession.getInstance().discoverMUCLightItems();

            if (discoverItems != null) {
                RealmManager.getInstance().hideAllMUCLightChats();
                List<DiscoverItems.Item> items = discoverItems.getItems();
                Realm realm = RealmManager.getInstance().getRealm();

                try {
                    for (DiscoverItems.Item item : items) {
                        String itemJid = item.getEntityID().toString();

                        if (itemJid.contains(XMPPSession.MUC_LIGHT_SERVICE_NAME)) {

                            Chat chatRoom = realm.where(Chat.class).equalTo("jid", itemJid).findFirst();

                            if (chatRoom == null) {
                                chatRoom = new Chat();
                                chatRoom.setJid(item.getEntityID().toString());
                                chatRoom.setType(Chat.TYPE_MUC_LIGHT);
                                getSubject(chatRoom);
                            }

                            realm.beginTransaction();
                            chatRoom.setShow(true);
                            chatRoom.setName(item.getName());
                            realm.copyToRealmOrUpdate(chatRoom);
                            realm.commitTransaction();

                            // set last retrieved from MAM
                            ChatMessage chatMessage = RealmManager.getInstance().getFirstMessageForChat(chatRoom.getJid());
                            if (chatMessage != null) {
                                realm.beginTransaction();
                                chatRoom.setLastRetrievedFromMAM(chatMessage.getMessageId());
                                realm.copyToRealmOrUpdate(chatRoom);
                                realm.commitTransaction();
                            }

                        }
                    }

                } finally {
                    realm.close();
                    mListener.onRoomsLoaded();
                }

            }
        }
    }