gpt4 book ai didi

ios - 在大型数据集上使用 RLMResults 进行快速枚举

转载 作者:行者123 更新时间:2023-11-29 00:26:10 28 4
gpt4 key购买 nike

我遇到了一个问题,我需要在相对较大的数据集(>7,000 项)上枚举 RLMResults 集合。我知道该 Realm 在访问其对象时会延迟加载其对象,但我遇到的问题是我需要访问集合中的每个项目,这会导致 7,000 多个项目中的每一个都被加载到内存中,从而导致内存溢出内存错误。根据 Realm 文档,他们不支持限制查询结果。

我可能需要做的一个例子是从文件系统中遍历并删除文件,是的,我可以使用谓词进行查询,并且只询问缓存的项目,但在最坏的情况下,查询可能会返回库中的所有项目。

RLMResults<DLLibrary *> *allItems = [DLLibrary allObjects];
for( DLLibrary *item in allItems ) {
// My understanding is that once the realm object is ready, it will be
// lazily loaded into memory. If I have many 1,000's of items in my data
// store this quickly becomes a problem memory wise.
if( item.isCached ) {
[[DLCacheService instance] deleteCachedFileWithDocumentId:item.id];
}
}

最佳答案

缓解这种情况的最简单方法是使用 @autoreleasepool 大括号来明确保证您延迟加载的对象在您检查完其内容后立即释放。 :)

RLMResults<DLLibrary *> *allItems = [DLLibrary allObjects];
for (NSInteger i = 0; i < allItems.count; i++) {
@autoreleasepool {
DLLibrary *item = allItems[i];
if (item.isCached) {
[[DLCacheService instance] deleteCachedFileWithDocumentId:item.id];
}
}
}

关于ios - 在大型数据集上使用 RLMResults 进行快速枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967365/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com