Fetch a single column from Realm Database (Android

2020-07-27 04:08发布

问题:

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