简体   繁体   中英

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?

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.

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]
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM