在核心数据NSFetchRequest中,正在获取NSInvalidArgumentException,无法识别的选择器



代码如下(我通过print语句隔离了它,错误发生在这些行中(:

let request = NSFetchRequest<Group>(entityName: "Group")
request.sortDescriptors = [NSSortDescriptor(key: "id", ascending: false)]
let groups = try! context.fetch(request)

注释(已编辑(:

  • SQLite后台存储
  • X代码12.3

我已经检查了基本情况,存在一个带有id字段的Group实体。

即使有一个简单的解决方案,也会非常感谢这里的错误修复策略。有没有什么方法可以让我自己阅读文档来弄清楚这一点?

完整堆栈跟踪:

2020-12-31 08:01:35.180433-0700 Things[25892:958568] -[__NSConcreteUUID compare:]: unrecognized selector sent to instance 0x60000367a9e0
2020-12-31 08:01:35.190552-0700 Things[25892:958568] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSConcreteUUID compare:]: unrecognized selector sent to instance 0x60000367a9e0'
*** First throw call stack:
(
0   CoreFoundation                      0x00007fff20420af6 __exceptionPreprocess + 242
1   libobjc.A.dylib                     0x00007fff20177e78 objc_exception_throw + 48
2   CoreFoundation                      0x00007fff2042f6f7 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
3   CoreFoundation                      0x00007fff20425036 ___forwarding___ + 1489
4   CoreFoundation                      0x00007fff20427068 _CF_forwarding_prep_0 + 120
5   Foundation                          0x00007fff2084132b _NSCompareObject + 46
6   CoreFoundation                      0x00007fff203aa09d __CFSimpleMergeSort + 74
7   CoreFoundation                      0x00007fff203aa133 __CFSimpleMergeSort + 224
8   CoreFoundation                      0x00007fff203a9c4a CFSortIndexes + 395
9   CoreFoundation                      0x00007fff203aa5f6 CFMergeSortArray + 288
10  Foundation                          0x00007fff208419d1 _sortedObjectsUsingDescriptors + 558
11  Foundation                          0x00007fff20841bbe -[NSArray(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 232
12  CoreData                            0x00007fff25113c7e -[NSManagedObjectContext executeFetchRequest:error:] + 4353
13  libswiftCoreData.dylib              0x00007fff54a88993 $sSo22NSManagedObjectContextC8CoreDataE5fetchySayxGSo14NSFetchRequestCyxGKSo0gH6ResultRzlF + 51
14  Things                              0x00000001048b7bdf $s6Things5GroupC6getAll7contextSayACGSo22NSManagedObjectContextC_tFZ + 591
15  Things                              0x0000000104872da7 $s6Things16thingDataToThing2td7contextAA0E0CAA0eC0V_So22NSManagedObjectContextCtF + 1399
16  Things                              0x00000001048836e5 $s6Things9APIServerC14syncWithRemote7context8callbackySo22NSManagedObjectContextC_yyctF14completionBothL_10groupDatas05thingN0ySayAA9GroupDataVG_SayAA05ThingQ0VGtF0O6MapperL_2tdAA0R0CAP_tF + 37
17  Things                              0x0000000104883908 $s6Things9ThingDataVAA0B0Cs5Error_pIggozo_AcEsAF_pIegnrzo_TR + 56
18  Things                              0x000000010488b034 $s6Things9ThingDataVAA0B0Cs5Error_pIggozo_AcEsAF_pIegnrzo_TRTA + 20
19  libswiftCore.dylib                  0x00007fff2f8edfe2 $sSlsE3mapySayqd__Gqd__7ElementQzKXEKlF + 674
20  Things                              0x000000010488313f $s6Things9APIServerC14syncWithRemote7context8callbackySo22NSManagedObjectContextC_yyctF14completionBothL_10groupDatas05thingN0ySayAA9GroupDataVG_SayAA05ThingQ0VGtF + 3855
21  Things                              0x0000000104883b0c $s6Things9APIServerC14syncWithRemote7context8callbackySo22NSManagedObjectContextC_yyctF16completionGroupsL_10groupDatasySayAA9GroupDataVG_tF0kA0L_05thingN0ySayAA05ThingP0VG_tF + 108
22  Things                              0x000000010488207f $sSay6Things9ThingDataVGIegg_ADIegn_TR + 15
23  Things                              0x000000010487ecfc $s6Things9APIServerC7request3url15queryParameters6parser0C6Method10completion12errorHandler12httpBodyJsonySS_SDyS2SGx10Foundation4DataVcAA11RequestTypeOyxcySScAOSgtlFyAR_So13NSURLResponseCSgs5Error_pSgtcfU_yycfU_ + 348
24  Things                              0x000000010488b463 $s6Things9APIServerC7request3url15queryParameters6parser0C6Method10completion12errorHandler12httpBodyJsonySS_SDyS2SGx10Foundation4DataVcAA11RequestTypeOyxcySScAOSgtlFyAR_So13NSURLResponseCSgs5Error_pSgtcfU_yycfU_TA + 51
25  Things                              0x000000010487ed70 $sIeg_IeyB_TR + 48
26  libdispatch.dylib                   0x0000000104c2d7ec _dispatch_call_block_and_release + 12
27  libdispatch.dylib                   0x0000000104c2e9c8 _dispatch_client_callout + 8
28  libdispatch.dylib                   0x0000000104c3ce75 _dispatch_main_queue_callback_4CF + 1152
29  CoreFoundation                      0x00007fff2038edbb __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
30  CoreFoundation                      0x00007fff2038963e __CFRunLoopRun + 2685
31  CoreFoundation                      0x00007fff203886d6 CFRunLoopRunSpecific + 567
32  GraphicsServices                    0x00007fff2bededb3 GSEventRunModal + 139
33  UIKitCore                           0x00007fff24690e0b -[UIApplication _run] + 912
34  UIKitCore                           0x00007fff24695cbc UIApplicationMain + 101
35  libswiftUIKit.dylib                 0x00007fff54d1e5f2 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
36  Things                              0x000000010489497a $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 122
37  Things                              0x00000001048948ee $s6Things11AppDelegateC5$mainyyFZ + 46
38  Things                              0x00000001048949c9 main + 41
39  libdyld.dylib                       0x00007fff202593e9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSConcreteUUID compare:]: unrecognized selector sent to instance 0x60000367a9e0'
terminating with uncaught exception of type NSException
CoreSimulator 732.18.6 - Device: iPhone 11 (93AB6812-3722-4022-B989-2E1423E6E6D0) - Runtime: iOS 14.3 (18C61) - DeviceType: iPhone 11

核心数据通常不支持对UUID字段进行排序。要使其工作,请将密钥路径更改为字符串类型,如下所示:

[NSSortDescriptor(key: "id.description", ascending: false)]

密钥路径id.uuidString理论上会更好,但这仍然会导致与原始问题中描述的相同的崩溃(至少在macOS 11中(。

最新更新