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

The following examples show how to use io.realm.Realm#copyToRealmOrUpdate() . 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: OpenLibre.java    From OpenLibre with GNU General Public License v3.0 6 votes vote down vote up
static void parseRawData() {
    Realm realmRawData = Realm.getInstance(realmConfigRawData);
    Realm realmProcessedData = Realm.getInstance(realmConfigProcessedData);

    // if processed data realm is empty
    if (realmProcessedData.isEmpty() && !realmRawData.isEmpty()) {
        // parse data from raw realm into processed data realm
        Log.i(LOG_ID, "Parsing data raw_data realm to processed_data realm.");
        realmProcessedData.beginTransaction();
        for (RawTagData rawTagData : realmRawData.where(RawTagData.class)
                        .findAllSorted(RawTagData.DATE, Sort.ASCENDING)) {
            realmProcessedData.copyToRealmOrUpdate(new ReadingData(rawTagData));
        }
        realmProcessedData.commitTransaction();
    }

    realmProcessedData.close();
    realmRawData.close();
}
 
Example 2
Source File: RealmUtility.java    From Loop with Apache License 2.0 6 votes vote down vote up
public static void saveQuery(String query){
    Realm realm = Realm.getDefaultInstance();
    try {
        RealmSuggestion realmSuggestion = new RealmSuggestion();
        realmSuggestion.setToken(query);
        realmSuggestion.setTimestamp(new Date());
        realm.beginTransaction();
        // This will create a new one in Realm
        // realm.copyToRealm(obj);
        // This will update a existing one with the same id or create a new one instead
        realm.copyToRealmOrUpdate(realmSuggestion);

        realm.commitTransaction();
    } finally {
        realm.close();
    }
}
 
Example 3
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 4
Source File: RoomRealm.java    From talk-android with MIT License 6 votes vote down vote up
public void addOrUpdateWithCurrentThread(final Room room) {
    if (room == null) return;
    Realm realm = RealmProvider.getInstance();
    try {
        realm.beginTransaction();
        copy(room, room);
        if (room.getIsGeneral()) {
            room.setTopic(MainApp.CONTEXT.getString(R.string.general));
        }
        realm.copyToRealmOrUpdate(room);
        realm.commitTransaction();
    } catch (Exception e) {
        e.printStackTrace();
        realm.cancelTransaction();
    } finally {
        realm.close();
    }
}
 
Example 5
Source File: XMPPSession.java    From mangosta-android with Apache License 2.0 6 votes vote down vote up
private void manageSender(String[] jidList, ChatMessage chatMessage, String chatRoomJid) {
    Realm realm = RealmManager.getInstance().getRealm();
    Chat chat = realm.where(Chat.class).equalTo("jid", chatRoomJid).findFirst();

    if (chat.getType() == Chat.TYPE_MUC_LIGHT) {
        if (jidList.length > 1) {
            chatMessage.setUserSender(XMPPUtils.fromJIDToUserName(jidList[1]));
        }

    } else {
        chatMessage.setUserSender(XMPPUtils.fromJIDToUserName(jidList[0]));

        if (!jidList[0].equals(Preferences.getInstance().getUserXMPPJid())) {
            String roomName = XMPPUtils.fromJIDToUserName(jidList[0]);
            realm.beginTransaction();
            chat.setName(roomName);
            realm.copyToRealmOrUpdate(chat);
            realm.commitTransaction();
        }

    }
}
 
Example 6
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 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: RealmManager.java    From mangosta-android with Apache License 2.0 6 votes vote down vote up
public String saveMessageLocally(Chat chat, String chatJID, String content, int type) {
    RoomsListManager.getInstance().createChatIfNotExists(chatJID, true);
    chat = RealmManager.getInstance().getChatFromRealm(getRealm(), chatJID);

    String messageId = UUID.randomUUID().toString();

    ChatMessage chatMessage = new ChatMessage();

    chatMessage.setMessageId(messageId);
    chatMessage.setRoomJid(chat.getJid());
    chatMessage.setUserSender(XMPPUtils.fromJIDToUserName(Preferences.getInstance().getUserXMPPJid()));
    chatMessage.setStatus(ChatMessage.STATUS_SENDING);
    chatMessage.setDate(new Date());
    chatMessage.setType(type);
    chatMessage.setContent(content);

    Realm realm = getRealm();
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(chatMessage);
    realm.commitTransaction();
    realm.close();

    return messageId;
}
 
Example 9
Source File: DatabaseRealm.java    From openwebnet-android with MIT License 5 votes vote down vote up
public <T extends RealmObject> T update(T model) {
    Realm realm = getRealmInstance();
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(model);
    realm.commitTransaction();
    return model;
}
 
Example 10
Source File: DatabaseHelper.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
public static void setSubreddits(Realm realm, User user, List<SubredditItem> items) {
    realm.beginTransaction();

    if(user.getSubreddits() != null){
        user.getSubreddits().clear();
    }else{
        user.setSubreddits(new RealmList<>());
    }
    for(SubredditItem item : items){
        user.getSubreddits().add(buildSubreddit(item));
    }

    realm.copyToRealmOrUpdate(user);
    realm.commitTransaction();
}
 
Example 11
Source File: RealmManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public void updateChatsSortPosition(List<Chat> chats) {
    Realm realm = getRealm();

    for (int i = 0; i < chats.size(); i++) {
        Chat chat = chats.get(i);

        realm.beginTransaction();
        chat.setSortPosition(i);
        realm.copyToRealmOrUpdate(chat);
        realm.commitTransaction();
    }
}
 
Example 12
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 13
Source File: DatabaseHelper.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
public static void setPrefs(Realm realm, AuthPrefs prefs) {
    User user = getSessionUser(realm);
    if(user != null){
        realm.beginTransaction();

        Prefs newPrefs = buildPrefs(prefs);
        newPrefs.setId(user.getId());

        realm.copyToRealmOrUpdate(newPrefs);
        realm.commitTransaction();
    }
}
 
Example 14
Source File: RealmPhoneContacts.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void addContactToDB(final StructListOfContact item, Realm realm) {
        try {
            RealmPhoneContacts realmPhoneContacts = new RealmPhoneContacts();
            realmPhoneContacts.setPhone(checkString(item));
//            realmPhoneContacts.setFirstName(item.firstName);
//            realmPhoneContacts.setLastName(item.lastName);
            realm.copyToRealmOrUpdate(realmPhoneContacts);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 15
Source File: RealmManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public void saveBlogPost(BlogPost blogPost) {
    Realm realm = getRealm();

    realm.beginTransaction();
    realm.copyToRealmOrUpdate(blogPost);
    realm.commitTransaction();

    realm.close();
}
 
Example 16
Source File: BloodGlucoseInputFragment.java    From OpenLibre with GNU General Public License v3.0 5 votes vote down vote up
public void saveBloodGlucoseLevel(long date, float bloodGlucoseLevel) {
    Realm realmUserData = Realm.getInstance(realmConfigUserData);
    realmUserData.beginTransaction();
    realmUserData.copyToRealmOrUpdate(new BloodGlucoseData(date, bloodGlucoseLevel));
    realmUserData.commitTransaction();
    realmUserData.close();
}
 
Example 17
Source File: RealmManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public void saveBlogPostComment(BlogPostComment comment) {
    Realm realm = getRealm();

    realm.beginTransaction();
    realm.copyToRealmOrUpdate(comment);
    realm.commitTransaction();

    realm.close();
}
 
Example 18
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 19
Source File: UserLogMessage.java    From 600SeriesAndroidUploader with MIT License 4 votes vote down vote up
@Override
public void execute(@NonNull Realm realm) {
    realm.copyToRealmOrUpdate(new UserLog().message(i, timestamp, type.value(), flag.value(), message));
}
 
Example 20
Source File: DatabaseHelper.java    From redgram-for-reddit with GNU General Public License v3.0 4 votes vote down vote up
private static void setSession(Realm realm, Session session) {
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(session);
    realm.commitTransaction();
}