带有自定义 NSSortDescriptor 的 NSFetchedResultsController
Posted
技术标签:
【中文标题】带有自定义 NSSortDescriptor 的 NSFetchedResultsController【英文标题】:NSFetchedResultsController with custom NSSortDescriptor 【发布时间】:2019-11-05 12:32:39 【问题描述】:我有一个带有firstName
、lastName
和category
(字符串)的Person
对象。
我想创建一个NSSortDescriptor
,它允许我获取按“category
”排序的所有Person
,然后按firstName
lastName
排序。
示例:
所需的顺序如下: 前景,INATIVE_1Y,GC,VGC。 然后,在类别中它们必须按字母顺序排列(姓氏,名字)
我尝试使用
NSSortDescriptor(key: , ascending: , comparator: )
,但没有成功。
下面是NSFetchRequest
的代码
let categoryDescriptor = NSSortDescriptor(key: "category", ascending: true) (category1, category2) -> ComparisonResult in
let arrayCategory = [PROSPECT, INATIVE_1Y, GC, VGC]
let c1Index = arrayCategory.firstIndex(of: category1) ?? 0
let c2Index = arrayCategory.firstIndex(of: category2) ?? 0
if c1Index < c2Index
return .orderedAscending
else if c1Index > c2Index
return .orderedDescending
return .orderedSame
objectFetchRequest.sortDescriptors?.append(categoryDescriptor)
let lastNameSortDescriptor = NSSortDescriptor(key: "lastName", ascending: true)
objectFetchRequest.sortDescriptors?.append(lastNameSortDescriptor)
let firstNameSortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)
objectFetchRequest.sortDescriptors?.append(firstNameSortDescriptor)
应用崩溃:
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLFetchRequestContext: 0x600002e82a00> , unsupported NSSortDescriptor (comparator blocks are not supported) with userInfo of (null)
2019-11-05 20:05:59.779136+0200 APP NAME [45129:1204138] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor (comparator blocks are not supported)'
*** First throw call stack:
(
0 CoreFoundation 0x0000000110efa8db __exceptionPreprocess + 331
1 libobjc.A.dylib 0x000000010f99fac5 objc_exception_throw + 48
2 CoreData 0x00000001109ba53e -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:nestIsWhereScoped:requestContext:] + 1646
3 CoreData 0x00000001109c4b0e -[NSSQLiteAdapter _statementForFetchRequestContext:ignoreInheritance:countOnly:nestingLevel:] + 142
4 CoreData 0x00000001108724c4 -[NSSQLiteAdapter newSelectStatementWithFetchRequestContext:ignoreInheritance:] + 116
5 CoreData 0x0000000110a0feb3 -[NSSQLFetchRequestContext _createStatement] + 67
6 CoreData 0x0000000110a0fe4e -[NSSQLFetchRequestContext fetchStatement] + 142
7 CoreData 0x0000000110a10e3b -[NSSQLFetchRequestContext executeRequestCore:] + 27
8 CoreData 0x0000000110a7a790 -[NSSQLStoreRequestContext executeRequestUsingConnection:] + 208
9 CoreData 0x0000000110a4f0eb __52-[NSSQLDefaultConnectionManager handleStoreRequest:]_block_invoke + 75
10 libdispatch.dylib 0x000000011271adb5 _dispatch_client_callout + 8
11 libdispatch.dylib 0x0000000112728d08 _dispatch_lane_barrier_sync_invoke_and_complete + 132
12 CoreData 0x0000000110a4efd0 -[NSSQLDefaultConnectionManager handleStoreRequest:] + 336
13 CoreData 0x0000000110a56c24 -[NSSQLCoreDispatchManager routeStoreRequest:] + 308
14 CoreData 0x000000011099f288 -[NSSQLCore dispatchRequest:withRetries:] + 232
15 CoreData 0x000000011099bffd -[NSSQLCore processFetchRequest:inContext:] + 93
16 CoreData 0x00000001108719ce -[NSSQLCore executeRequest:withContext:error:] + 574
17 CoreData 0x0000000110980657 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 2039
18 CoreData 0x0000000110978870 -[NSPersistentStoreCoordinator _routeHeavyweightBlock:] + 288
19 CoreData 0x00000001108711a0 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1296
20 CoreData 0x000000011086f475 -[NSManagedObjectContext executeFetchRequest:error:] + 933
21 CoreData 0x0000000110a1f9c0 __43-[NSFetchedResultsController performFetch:]_block_invoke + 448
22 CoreData 0x000000011098d104 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 212
23 libdispatch.dylib 0x000000011271adb5 _dispatch_client_callout + 8
24 libdispatch.dylib 0x0000000112728d08 _dispatch_lane_barrier_sync_invoke_and_complete + 132
25 CoreData 0x0000000110978146 -[NSPersistentStoreCoordinator performBlockAndWait:] + 198
26 CoreData 0x00000001108b2a2a developerSubmittedBlockToNSManagedObjectContextPerform + 170
27 CoreData 0x00000001108b28ff -[NSManagedObjectContext performBlockAndWait:] + 239
28 CoreData 0x0000000110a1fd11 -[NSFetchedResultsController _recursivePerformBlockAndWait:withContext:] + 145
29 CoreData 0x0000000110a1f72b -[NSFetchedResultsController performFetch:] + 299
30 APP NAME 0x0000000109f25278 $s20APP_NAME22GeneralFetchControllerC14refreshResultsyySSF + 1512
31 APP NAME 0x0000000109daca74 $s20APP_NAME19LocalClientSearchVCC14refreshResultsyySSF + 164
32 APP NAME 0x0000000109f24b8b $s20APP_NAME22GeneralFetchControllerC024initializeFetchedResultsG0yyF + 619
33 APP NAME 0x0000000109f247fa $s20APP_NAME22GeneralFetchControllerC11viewDidLoadyyFyycfU_ + 58
34 APP NAME 0x0000000109f2487d $s20APP_NAME22GeneralFetchControllerC11viewDidLoadyyFyycfU_TA + 13
35 APP NAME 0x0000000109c78c4d $sIeg_IeyB_TR + 45
36 libdispatch.dylib 0x0000000112719d7f _dispatch_call_block_and_release + 12
37 libdispatch.dylib 0x000000011271adb5 _dispatch_client_callout + 8
38 libdispatch.dylib 0x0000000112728080 _dispatch_main_queue_callback_4CF + 1540
39 CoreFoundation 0x0000000110e61a79 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
40 CoreFoundation 0x0000000110e5c126 __CFRunLoopRun + 2310
41 CoreFoundation 0x0000000110e5b4d2 CFRunLoopRunSpecific + 626
42 GraphicsServices 0x00000001140b42fe GSEventRunModal + 65
43 UIKitCore 0x000000011a14ffc2 UIApplicationMain + 140
44 APP NAME 0x000000010a307ffb main + 75
45 libdyld.dylib 0x000000011278f541 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
没有categorySortDescriptor
,应用程序不会崩溃。
感谢您的帮助
【问题讨论】:
【参考方案1】:有(至少)两种选择:
-
为类别订单添加索引属性,并在设置类别属性时更新。
添加一个计算属性以以正确的顺序从数组中返回索引,例如安全版本
var categoryIndex : Int
switch category
case "PROSPECT": return 0
case "INATIVE_1Y": return 1
case "GC": return 2
case "VGC": return 3
default: fatalError("category not available")
然后按该索引排序。
NSSortDescriptor(key:ascending:comparator:
不起作用,因为 SQL 请求中不支持基于块的 API,请参阅错误消息
不支持比较块
【讨论】:
您好@vadian 感谢您的帮助!索引是个好主意!不过,我想尝试使用 NSSortDescriptor。我用代码和崩溃更新了问题。谢谢 我错了,NSSortDescriptor(key:ascending:comparator:
不起作用,注意错误信息comparator blocks are not supported
好的,谢谢,你能更详细地解释一下第二种解决方案吗?以上是关于带有自定义 NSSortDescriptor 的 NSFetchedResultsController的主要内容,如果未能解决你的问题,请参考以下文章
使用 NSSortDescriptor 按时间错误排序(带有核心数据)
核心数据 - 可以使用 NSSortDescriptor 中的块对 TableView 进行排序吗?
为啥没有多个 NSSortDescriptor 与 NSFetchedResultsController 一起工作
NSFetchedResultsController,分组 UITableView、HeaderInSection 和 NSSortDescriptor