首页 > 解决方案 > How to retrieve data in Realm based on a given index range?

问题描述

I want to retrieve data in my Realm database based on a given index range. I did some research and found that you can do something like this in Realm for java:

realm.where(clazz).between("count",0,1000).findAll(); // Get the first 1000 data

But I have yet to find similar function in Realm for swift. Here's my get all function:

func getDataBasedOnIndex() -> Results<DataModel> {
    let realm = try! Realm()
    let localData =  realm.objects(DataModel.self)
    let sortProperties = [
        SortDescriptor (keyPath: "time", ascending: false)
    ]
    return localData.sorted(by: sortProperties)
}

Based on the function above, I want to make pagination when retrieving my data. For example, I have a UITableView filled with the data retrieved from my realm database. When the page first loaded, I only want to show 10 data from the database, but as the user scrolls to the bottom of the table view, the data keeps increasing until it runs out. How do I manage to do the same thing in Realm for swift?

标签: swiftrealm

解决方案


There is no need to implement pagination in realm queries, because they are "lazy" from the box. It means objects are loaded from query only when you are accessing them.

SDK reference


推荐阅读