Viraj Phadte
Viraj Phadte

Reputation: 76

Does realm support LIMIT query in iOS?

I am storing list of contacts from Phone Addressbook locally in my project and for that I am using realm db, problem now I am getting is to fetch batch of contacts (like a pagination). So thinking to do it using limit query. But there is no example with LIMIT query with realm. Is there any alternative for this to do pagination in realm?

Upvotes: 3

Views: 5758

Answers (2)

Robin M.
Robin M.

Reputation: 96

You do not need to implement fetching batches on your own as Realm Swift Queries are lazily loaded. "All queries (including queries and property access) are lazy in Realm. Data is only read when the properties are accessed."

So your query is very fast but accessing the data itself isn't as fast as using an array.

Upvotes: 4

Quoc Nguyen
Quoc Nguyen

Reputation: 3007

In the Realm swift's document site (https://realm.io/docs/swift/latest/), they said

Since queries in Realm are lazy, performing this sort of paginating behavior isn’t necessary at all, as Realm will only load objects from the results of the query once they are explicitly accessed.

If for UI-related or other implementation reasons you require a specific subset of objects from a query, it’s as simple as taking the Results object, and reading out only the objects you need.

So you just simple get it all and process what you need. Example from Document site

// Loop through the first 5 Dog objects
// restricting the number of objects read from disk
let dogs = try! Realm().objects(Dog.self)
for i in 0..<5 {
    let dog = dogs[i]
    // ...
}

Upvotes: 4

Related Questions