Ranjithkumar
Ranjithkumar

Reputation: 18386

Fetch a single column from Realm Database (Android)

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++){ 
}

Upvotes: 4

Views: 8892

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81539

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();

Upvotes: 5

Sergei Bubenshchikov
Sergei Bubenshchikov

Reputation: 5361

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

Upvotes: 7

Related Questions