如何从 Realm 数据库中获取部分数据
How to get data partially from Realm database
一些如何从领域部分获取数据,android 屏幕的主要部分无法显示超过 10-20 个项目,因此当用户需要时没有必要加载 1000 行只看到 10-20 个。
所以我找到了一个如何像这样部分获取数据的解决方案:
FetchDocumentsCommand = new MvxCommand(
() =>
{
if (//Here I need to find a way to get data for example if I had it as pages from page 1/2/3)
{
//The load would execute for some specific page and add it to an ObservableCollection
//So it becomes less laggy
FetchDocumentTask = MvxNotifyTask.Create(LoadDocuments);
RaisePropertyChanged(() => FetchDocumentTask);
}
});
当我尝试为我从 Realm 获取数据时出现问题,它看起来像这样
private async Task LoadDocuments()
{
var docs = _documentService.GetRealmDocuments();
Documents.AddRange(docs);
}
public IEnumerable<Document> GetRealmDocuments()
{
return RealmService.Realm.All<Document>();
}
我的问题是,当我从领域获取它时,如何减少分页?
Realm 对象是延迟加载的,因此加载 10 个对象或 1000 个对象之间基本上没有区别。来自文档
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.
如果您确实希望对某些 UI 功能进行分页,只需填充一个结果对象,然后只访问您需要的数据量即可。从概念上讲,它看起来像这样
// load the dogs and only display the first 5
let dogs = realm.objects(Dog.self)
for i in 0..<5 {
let dog = dogs[i]
// display dog
}
不过要记住一些事情 - Realm 结果对象不保证顺序,因此您应该在读取
中的对象时添加某种顺序
let dogs = realm.objects(Dog.self).sorted(by: "dog_name")
当狗被添加、修改或删除时,这将使您的订单保持不变。
一些如何从领域部分获取数据,android 屏幕的主要部分无法显示超过 10-20 个项目,因此当用户需要时没有必要加载 1000 行只看到 10-20 个。 所以我找到了一个如何像这样部分获取数据的解决方案:
FetchDocumentsCommand = new MvxCommand(
() =>
{
if (//Here I need to find a way to get data for example if I had it as pages from page 1/2/3)
{
//The load would execute for some specific page and add it to an ObservableCollection
//So it becomes less laggy
FetchDocumentTask = MvxNotifyTask.Create(LoadDocuments);
RaisePropertyChanged(() => FetchDocumentTask);
}
});
当我尝试为我从 Realm 获取数据时出现问题,它看起来像这样
private async Task LoadDocuments()
{
var docs = _documentService.GetRealmDocuments();
Documents.AddRange(docs);
}
public IEnumerable<Document> GetRealmDocuments()
{
return RealmService.Realm.All<Document>();
}
我的问题是,当我从领域获取它时,如何减少分页?
Realm 对象是延迟加载的,因此加载 10 个对象或 1000 个对象之间基本上没有区别。来自文档
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.
如果您确实希望对某些 UI 功能进行分页,只需填充一个结果对象,然后只访问您需要的数据量即可。从概念上讲,它看起来像这样
// load the dogs and only display the first 5
let dogs = realm.objects(Dog.self)
for i in 0..<5 {
let dog = dogs[i]
// display dog
}
不过要记住一些事情 - Realm 结果对象不保证顺序,因此您应该在读取
中的对象时添加某种顺序let dogs = realm.objects(Dog.self).sorted(by: "dog_name")
当狗被添加、修改或删除时,这将使您的订单保持不变。