Swift 5.1 将核心数据添加到现有项目但我收到此错误
Swit 5.1 Adding CoreData to an Existing project But I got this error
2020-04-05 16:18:10.014238+0530 StayHome[2732:178019] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'NotifyDetails' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'NotifyDetails' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
2020-04-05 16:18:10.014435+0530 StayHome[2732:178019] [error] error: +[NotifyDetails entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[NotifyDetails entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
2020-04-05 16:18:10.016103+0530 StayHome[2732:178019] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObject of class 'NotifyDetails' must have a valid NSEntityDescription.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000110cb527e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x000000010c3e5b20 objc_exception_throw + 48
2 CoreData 0x000000010c939d28 -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:] + 485
3 StayHome 0x000000010b94470f $s8StayHome13NotifyDetailsC6entity10insertIntoACSo19NSEntityDescriptionC_So22NSManagedObjectContextCSgtcfc + 111
4 StayHome 0x000000010b9447fc $s8StayHome13NotifyDetailsC6entity10insertIntoACSo19NSEntityDescriptionC_So22NSManagedObjectContextCSgtcfcTo + 76
5 CoreData 0x000000010c9e767e -[NSManagedObject initWithContext:] + 290
6 StayHome 0x000000010b90484f $sSo15NSManagedObjectC7contextABSo0aB7ContextC_tcfcTO + 31
7 StayHome 0x000000010b9042b7 $sSo15NSManagedObjectC7contextABSo0aB7ContextC_tcfC + 39
8 StayHome 0x000000010b903f7a $s8StayHome13LocalDatabaseV0C4DatayySS_SStF + 282
9 StayHome 0x000000010b9079ef $s8StayHome17LocalNotificationC0D0yySS_SStFySb_s5Error_pSgtcfU_ + 223
10 StayHome 0x000000010b907f6f $sSbs5Error_pSgIegyg_SbSo7NSErrorCSgIeyByy_TR + 127
11 libdispatch.dylib 0x0000000111afedd4 _dispatch_call_block_and_release + 12
12 libdispatch.dylib 0x0000000111affd48 _dispatch_client_callout + 8
13 libdispatch.dylib 0x0000000111b065ef _dispatch_lane_serial_drain + 788
14 libdispatch.dylib 0x0000000111b071b5 _dispatch_lane_invoke + 476
15 libdispatch.dylib 0x0000000111b12a4e _dispatch_workloop_worker_thread + 719
16 libsystem_pthread.dylib 0x0000000111f7c6fc _pthread_wqthread + 290
17 libsystem_pthread.dylib 0x0000000111f7b827 start_wqthread + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我遇到了同样的问题并尝试了多种解决方案,例如注释掉
@objc(CheatTime)
或在 .xcdatamodeld 中选择当前产品模块
没有任何效果。
在那之后,我尝试在添加新对象之前发出获取请求并且成功了
所以在我的案例中,我添加了以下代码:
let request = CheatTime.fetchRequest() as NSFetchRequest<CheatTime>
request.sortDescriptors = [NSSortDescriptor(key: #keyPath(CheatTime.numberOfHours), ascending: true)]
let fetchedRC = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
try? fetchedRC.performFetch()
之后添加对象没有问题,不仅是第一个对象类型(在我的示例中是 CheatTime),而且第二个对象类型也是如此(没有额外的获取请求)
2020-04-05 16:18:10.014238+0530 StayHome[2732:178019] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'NotifyDetails' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'NotifyDetails' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
2020-04-05 16:18:10.014435+0530 StayHome[2732:178019] [error] error: +[NotifyDetails entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[NotifyDetails entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
2020-04-05 16:18:10.016103+0530 StayHome[2732:178019] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObject of class 'NotifyDetails' must have a valid NSEntityDescription.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000110cb527e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x000000010c3e5b20 objc_exception_throw + 48
2 CoreData 0x000000010c939d28 -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:] + 485
3 StayHome 0x000000010b94470f $s8StayHome13NotifyDetailsC6entity10insertIntoACSo19NSEntityDescriptionC_So22NSManagedObjectContextCSgtcfc + 111
4 StayHome 0x000000010b9447fc $s8StayHome13NotifyDetailsC6entity10insertIntoACSo19NSEntityDescriptionC_So22NSManagedObjectContextCSgtcfcTo + 76
5 CoreData 0x000000010c9e767e -[NSManagedObject initWithContext:] + 290
6 StayHome 0x000000010b90484f $sSo15NSManagedObjectC7contextABSo0aB7ContextC_tcfcTO + 31
7 StayHome 0x000000010b9042b7 $sSo15NSManagedObjectC7contextABSo0aB7ContextC_tcfC + 39
8 StayHome 0x000000010b903f7a $s8StayHome13LocalDatabaseV0C4DatayySS_SStF + 282
9 StayHome 0x000000010b9079ef $s8StayHome17LocalNotificationC0D0yySS_SStFySb_s5Error_pSgtcfU_ + 223
10 StayHome 0x000000010b907f6f $sSbs5Error_pSgIegyg_SbSo7NSErrorCSgIeyByy_TR + 127
11 libdispatch.dylib 0x0000000111afedd4 _dispatch_call_block_and_release + 12
12 libdispatch.dylib 0x0000000111affd48 _dispatch_client_callout + 8
13 libdispatch.dylib 0x0000000111b065ef _dispatch_lane_serial_drain + 788
14 libdispatch.dylib 0x0000000111b071b5 _dispatch_lane_invoke + 476
15 libdispatch.dylib 0x0000000111b12a4e _dispatch_workloop_worker_thread + 719
16 libsystem_pthread.dylib 0x0000000111f7c6fc _pthread_wqthread + 290
17 libsystem_pthread.dylib 0x0000000111f7b827 start_wqthread + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我遇到了同样的问题并尝试了多种解决方案,例如注释掉
@objc(CheatTime)
或在 .xcdatamodeld 中选择当前产品模块
没有任何效果。
在那之后,我尝试在添加新对象之前发出获取请求并且成功了 所以在我的案例中,我添加了以下代码:
let request = CheatTime.fetchRequest() as NSFetchRequest<CheatTime>
request.sortDescriptors = [NSSortDescriptor(key: #keyPath(CheatTime.numberOfHours), ascending: true)]
let fetchedRC = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
try? fetchedRC.performFetch()
之后添加对象没有问题,不仅是第一个对象类型(在我的示例中是 CheatTime),而且第二个对象类型也是如此(没有额外的获取请求)