简体   繁体   English

Realm 是否支持 iOS 中的 LIMIT 查询?

[英]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.但是没有使用领域的 LIMIT 查询的例子。 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 .由于Realm Swift 查询是延迟加载的,因此您不需要自己实现批处理。 "All queries (including queries and property access) are lazy in Realm. Data is only read when the properties are accessed." “所有查询(包括查询和属性访问)在 Realm 中都是惰性的。只有在访问属性时才会读取数据。”

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在 Realm swift 的文档站点( https://realm.io/docs/swift/latest/ )中,他们说

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.由于 Realm 中的查询是惰性的,因此根本不需要执行这种分页行为,因为 Realm 只会从查询结果中加载对象,一旦它们被显式访问。

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.如果出于 UI 相关或其他实现原因,您需要查询中的特定对象子集,则只需获取 Results 对象,然后仅读出您需要的对象即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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