问题描述
我有一些对象需要以无法使用排序描述符表达的方式进行排序(它涉及树中的位置).目前,我正在为它们排序一个后台线程,收集它们的objectID,然后将它们返回到主上下文中,如下所示:
I have some objects which I need to sort in a way that I can't express with sort descriptors (it involves position in a tree). At the moment I'm sorting them a background thread, collecting their objectIDs, and then returning them to the main context like so:
for (NSManagedObjectID *objectID in objectIDs)
{
NSManagedObject *managedObject = [mainContext existingObjectWithID:objectID error:outError];
if (!managedObject) return nil;
[array addObject:managedObject];
}
这涉及很多数据库命中,所以我宁愿一次完成.《核心数据编程指南》 建议使用这样的谓词:
This involves a lot of DB hits, so I'd prefer to do it in one go. The Core Data Programming Guide recommends doing so using a predicate like this:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[MyEntity entityName]];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", objectIDs];
NSArray *array = [mainContext executeFetchRequest:fetchRequest error:&error];
但是,我担心我会失去顺序,因为在其他地方,他们使用 @"SELF IN%@"
并提供排序描述符.核心数据批量故障是否保留顺序?
However, I'm worried that I'll lose the ordering, because elsewhere they use a @"SELF IN %@"
and provide sort descriptors. Does core data batch faulting preserve ordering?
推荐答案
带有谓词的获取请求
[NSPredicate predicateWithFormat:@"SELF IN %@", objectIDs]
不按与存储在数组中的对象ID相同的顺序不返回对象.我做了一个小测试:
does not return the objects in the same order as the object IDs are stored in the array. I made a small test:
NSLog(@"%@", objectIDs);
// "0x102005370 <x-coredata://FA77144F-F37B-455C-AC6F-18563A34C4A7/Question/p5>",
// "0x102003700 <x-coredata://FA77144F-F37B-455C-AC6F-18563A34C4A7/Question/p4>",
// "0x1020036f0 <x-coredata://FA77144F-F37B-455C-AC6F-18563A34C4A7/Question/p1>"
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Question"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", objectIDs];
NSArray *array = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"%@", array);
// "<Question: 0x10201ae10> (entity: Question; id: 0x1020036f0 <x-coredata://FA77144F-F37B-455C-AC6F-18563A34C4A7/Question/p1> ; data: <fault>)",
// "<Question: 0x10201afe0> (entity: Question; id: 0x102003700 <x-coredata://FA77144F-F37B-455C-AC6F-18563A34C4A7/Question/p4> ; data: <fault>)",
// "<Question: 0x10201b060> (entity: Question; id: 0x102005370 <x-coredata://FA77144F-F37B-455C-AC6F-18563A34C4A7/Question/p5> ; data: <fault>)"
通过设置启动参数"-com.apple.CoreData.SQLDebug 3",可以看出获取请求已转换为SQLite查询
By setting the launch argument "-com.apple.CoreData.SQLDebug 3", it can be seen that the fetch request is translated to the SQLite query
SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZTEXT, t0.ZTITLE FROM ZQUESTION t0 WHERE t0.Z_PK IN (5,4,1)
将测试 t0.Z_PK IN(5,4,1)
应用于表中的所有行.因此,提取请求数组中对象的顺序对返回的对象的顺序没有影响.
which applies the test t0.Z_PK IN (5,4,1)
to all rows in the table. So the order of the objects in the array of the fetch request has no influence on the order of the returned objects.
这篇关于核心数据批量故障是否保留顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!