具有自定义NSSortDescriptor的NSFetchedResultsController

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

我想创建一个NSSortDescriptor,它使我可以提取按“ Person”排序的所有category,然后按firstName lastName排序。

示例:

具有自定义NSSortDescriptor的NSFetchedResultsController

所需顺序如下: 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的情况下,应用程序不会崩溃。

感谢您的帮助

z156578453 回答:具有自定义NSSortDescriptor的NSFetchedResultsController

(至少)有两个选项:

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

  

不支持比较器块

本文链接:https://www.f2er.com/3159648.html

大家都在问