У меня есть Person
объект с firstName
, lastName
и category
(String).
Я хотел бы создать NSSortDescriptor
, который позволит мне получить все Person
отсортировано по "category
" и после этого отсортировано по firstName
lastName
.
Пример:
Требуется следующий порядок: PROSPECT, INATIVE_1Y, GC, VGC. Затем в категории они должны быть расположены в алфавитном порядке (lastName, firstName)
Я пытался с 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
приложение не сбоит.
Спасибо за помощь