swift - 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?
解决方案
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.
推荐阅读
- c# - 在列表中查找具有特定长度的数组的索引?
- asp.net - 允许用户在 Authorize[] middleare .Net Core Api 中仅使用 id 访问他/她自己的资源
- c# - 如何使用 C# 连接多个 excel 列
- c# - 如果这些游戏对象与 UI 无关,是否可以将 Canvas 对象 (rectTransform) 与常规游戏对象 (transform) 混合?
- node.js - TypeError: webpack.DefinePlugin 不是构造函数
- asp.net-core-webapi - 是否可以检索受 Dapper 查询影响的行数?
- go - 导入模块位于 Goland IDE 的子文件夹中
- r - rstudio如何选择在启动时/项目关闭时打开哪些文件
- algorithm - 使用具有重复值的中序和预序构造二叉树
- java - 从 Excel 导出的 CSV (UTF-8) 文件的默认行终止符是什么?