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

The following examples show how to use io.realm.Realm#copyFromRealm() . 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: FavoriteArticlesDataLocalSource.java    From WanAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isExist(int userId, int id) {
    Realm realm = RealmHelper.newRealmInstance();
    LoginDetailData data = realm.copyFromRealm(
            realm.where(LoginDetailData.class)
                    .equalTo("id", userId)
                    .findFirst()
    );
    List<Integer> collectIds = data.getCollectIds();

    if (collectIds.isEmpty()) {
        return false;
    }

    for (Integer collectId : collectIds) {
        if (id == collectId) {
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: ReadLaterArticlesLocalSource.java    From WanAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public Observable<List<ReadLaterArticleData>> getReadLaterArticles(int userId) {
    Realm realm = RealmHelper.newRealmInstance();
    List<ReadLaterArticleData> datas = realm.copyFromRealm(realm.where(ReadLaterArticleData.class)
            .equalTo("userId", userId)
            .findAll());
    return Observable.fromIterable(datas).toSortedList(new Comparator<ReadLaterArticleData>() {
        @Override
        public int compare(ReadLaterArticleData data, ReadLaterArticleData t1) {
            if (data.getTimestamp() > t1.getTimestamp()) {
                return -1;
            } else {
                return 1;
            }
        }
    }).toObservable();

}
 
Example 3
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 4
Source File: WidgetListFactory.java    From WanAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public RemoteViews getViewAt(int i) {
    RemoteViews remoteViews = new RemoteViews(
            context.getPackageName(), R.layout.item_list_app_widget);
    Realm realm=Realm.getInstance(new RealmConfiguration.Builder()
            .deleteRealmIfMigrationNeeded()
            .name(RealmHelper.DATABASE_NAME)
            .build());
    List<ReadLaterArticleData> list = realm.copyFromRealm(
            realm.where(ReadLaterArticleData.class)
                    .equalTo("userId", userId)
                    .sort("timestamp", Sort.DESCENDING)
                    .findAll());
    ReadLaterArticleData data = list.get(i);
    remoteViews.setTextViewText(R.id.text_view_author,data.getAuthor());
    remoteViews.setTextViewText(R.id.text_view_title, StringUtil.replaceInvalidChar(data.getTitle()));
    Intent intent = new Intent();
    intent.putExtra(DetailActivity.ID, data.getId());
    intent.putExtra(DetailActivity.TITLE, data.getTitle());
    intent.putExtra(DetailActivity.URL, data.getLink());
    intent.putExtra(DetailActivity.FROM_FAVORITE_FRAGMENT, false);
    intent.putExtra(DetailActivity.FROM_BANNER, false);
    remoteViews.setOnClickFillInIntent(R.id.item_main, intent);
    return remoteViews;
}
 
Example 5
Source File: LoginDataLocalSource.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<LoginDetailData> getLocalLoginData(int userId) {
    Realm realm = RealmHelper.newRealmInstance();
    LoginDetailData loginDetailData = realm.copyFromRealm(
            realm.where(LoginDetailData.class)
                    .equalTo("id", userId)
                    .findFirst());
    return Observable.just(loginDetailData);
}
 
Example 6
Source File: ChampionManager.java    From Theogony with MIT License 5 votes vote down vote up
public List<Champion> getAll() {
    Realm realm = RealmProvider.getInstance().getRealm();
    try {
        RealmResults<Champion> results = realm
                .where(Champion.class).findAll();
        return realm.copyFromRealm(results);
    } finally {
        realm.close();
    }
}
 
Example 7
Source File: ChampionManager.java    From Theogony with MIT License 5 votes vote down vote up
public List<Champion> queryByTag(String tag) {
    if (TextUtils.isEmpty(tag)) {
        return getAll();
    }
    Realm realm = RealmProvider.getInstance().getRealm();
    try {
        RealmResults<Champion> results = realm
                .where(Champion.class).contains("tagsc", tag).findAll();
        return realm.copyFromRealm(results);
    } finally {
        realm.close();
    }
}
 
Example 8
Source File: ChampionManager.java    From Theogony with MIT License 5 votes vote down vote up
public List<Champion> queryByKeyWord(String query) {
    Realm realm = RealmProvider.getInstance().getRealm();
    try {
        RealmResults<Champion> results = realm.where(Champion.class)
                .beginGroup()
                .contains("name", query)
                .or()
                .contains("title", query)
                .endGroup()
                .findAll();
        return realm.copyFromRealm(results);
    } finally {
        realm.close();
    }
}
 
Example 9
Source File: DatabaseManager.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
private void initCurrentAccessToken() {
    Realm realm = getInstance();
    Token token = DatabaseHelper.getSessionToken(realm);
    if(token != null){
        this.currentAccessToken = realm.copyFromRealm(token);
    }
}
 
Example 10
Source File: DatabaseManager.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
public Session getSession(){
    Realm realm = getInstance();
    Session session = DatabaseHelper.getSession(realm);
    Session usableSession = realm.copyFromRealm(session);
    close(realm);
    return usableSession;
}
 
Example 11
Source File: DatabaseManager.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
public User getSessionUser(){
    Realm realm = getInstance();
    User user = DatabaseHelper.getSessionUser(realm);
    User usableUser = null;
    if(user != null){
        usableUser = realm.copyFromRealm(user);
    }
    close(realm);
    return usableUser;
}
 
Example 12
Source File: DatabaseManager.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
public List<Subreddit> getSubreddits() {
    Realm realm = getInstance();
    RealmList<Subreddit> subreddits = DatabaseHelper.getSubreddits(realm);
    List<Subreddit> usableList = realm.copyFromRealm(subreddits);
    close(realm);
    return usableList;
}
 
Example 13
Source File: DatabaseManager.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
public List<Subreddit> getSubreddits(String userId) {
    Realm realm = getInstance();
    RealmList<Subreddit> subreddits = DatabaseHelper.getSubreddits(realm, userId);
    List<Subreddit> usableList = realm.copyFromRealm(subreddits);
    close(realm);
    return usableList;
}
 
Example 14
Source File: Stats.java    From 600SeriesAndroidUploader with MIT License 4 votes vote down vote up
public StatInterface[] readRecords(Class[] classes, String key, boolean isWrite) {
    Realm storeRealm = null;
    StatInterface[] loaded = new StatInterface[classes.length];

    int i = 0;
    for (Class clazz : classes) {
        boolean load = true;

        for (LoadedRecord loadedRecord : loadedRecords) {
            if (loadedRecord.statRecord.getClass().equals(clazz) && loadedRecord.statRecord.getKey().equals(key)) {
                loadedRecord.isWrite |= isWrite;
                loaded[i++] = loadedRecord.statRecord;
                load = false;
                break;
            }
        }

        if (load) {
            if (storeRealm == null)
                storeRealm = Realm.getInstance(UploaderApplication.getStoreConfiguration());

            StatInterface record = (StatInterface) storeRealm.where(clazz)
                    .equalTo("key", key)
                    .findFirst();

            if (record == null) {
                Log.d(TAG, String.format("create: %s key: %s write: %s", clazz.getSimpleName(), key, isWrite));
                try {
                    record = (StatInterface) clazz.getConstructor().newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(TAG + " could not construct new class");
                }
                record.setKey(key);
                record.setDate(new Date(System.currentTimeMillis()));
            } else {
                Log.d(TAG, String.format("read: %s key: %s write: %s", clazz.getSimpleName(), key, isWrite));
                record = storeRealm.copyFromRealm(record);
            }

            loadedRecords.add(new LoadedRecord(record, isWrite));
            loaded[i++] = record;
        }
    }

    if (storeRealm != null) storeRealm.close();
    return loaded;
}
 
Example 15
Source File: AccountManager.java    From quill with MIT License 4 votes vote down vote up
public static List<BlogMetadata> getAllBlogs() {
    final Realm realm = Realm.getDefaultInstance();
    return realm.copyFromRealm(realm
            .where(BlogMetadata.class)
            .findAll());
}