Fetch a single column from Realm Database (Android)

强颜欢笑 提交于 2019-12-21 21:52:58

问题


I'm a beginner in Realm.

I have a table with 3 columns which named Id, Name, Email,Address. To get the data of Name column, we use a query like 'SELECT Name from table_name' for SQLite.

If we using Realm in Android, then which method do we have to use for fetching the data of only one column?

I searched alot on Google & documentation but to no avail. Could anyone help me?

Update:

What I am tried:

RealmResults<User> results = query.findAll();
ArrayList<String> name = new Arraylist(); 
for(i=0; i<results.size; i++){ 
 name.add(result.get(i).getName();
}

My problem:

results.size()  > 10k. So I want to avoid 10k iteration

for(i=0; i<results.size; i++){ 
}

回答1:


If we using Realm in Android, then which method do we have to use for fetching the data of only one column?

You can't, because Realm is an object store, it doesn't have concept of "columns".


My problem:

results.size() > 10k. So I want to avoid 10k iteration

for(i = 0; i < results.size(); i++){ 
}

Solution: don't iterate?

 RealmResults<User> results = query.findAll();
 //List<String> name = new ArrayList<>(); 
 //for(i = 0; i < results.size(); i++){ 
 //     name.add(result.get(i).getName();
 //}
 return results;

 // ...
 String name = results.get(position).getName();



回答2:


Look at queries section at the documentation:

All fetches (including queries) are lazy in Realm, and the data is never copied.

This mean, that data of particular column (property) will be fetched when you call getMyProperty() method. Not after call of finadAll() method of RealmQuery object



来源:https://stackoverflow.com/questions/42640753/fetch-a-single-column-from-realm-database-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!