ios - 具有自定义 NSSortDescriptor 的 NSFetchedResultsController

标签 ios core-data nssortdescriptor

我有一个 Person 对象,其中包含 firstNamelastNamecategory(字符串)。

我想创建一个NSSortDescriptor,它允许我获取按“category”排序的所有Person,然后按排序>名字 姓氏

示例:

enter image description here

所需的顺序如下: 前景、INATIVE_1Y、GC、VGC。 然后,在类别中它们必须按字母顺序排列(lastName,firstName)

I tried with NSSortDescriptor(key: , ascending: , comparator: ) but without success.

在我的 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. 为类别顺序添加一个索引属性,并在设置类别属性时更新它。
  2. 添加计算属性以按正确顺序从数组返回索引,例如安全版本

    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 请求不支持基于 block 的 API,请参阅错误消息

comparator blocks are not supported

关于ios - 具有自定义 NSSortDescriptor 的 NSFetchedResultsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58711439/

相关文章:

ios - 我不完全理解 sendSubviewToBack。请看代码

ios - NSPredicate 不适用于计算字段

ios - 如何在一个 View Controller 中为 TableView 分配标签值

ios - 我删除了包含 UIView Controller 类 * 的类文件。和 *.h 文件

iphone - 过滤NSFetchedResultsController以删除具有相同名称的对象

iphone - 核心数据 - 迁移问题?

ios - 根据 Objective-C 中的评论数量对核心数据中的 FetchedObjects 数组进行排序

ios - NSComparator block 未调用

ios - 过滤包含消息列表的 NSMutableArray

iOS - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序