CoreData设置关系给出错误' - [WeatherObservation count]:发送到实例的无法识别的选择器

Kev*_*vin 6 core-data ios

WeatherObservation我是数据模型中的一个实体,我试图将它与另一个Object(一对一)的关系设置为我通过导航控制器传递的.几个星期前就有用了.我更改了模型中的一些其他实体,重新生成了类,并将此关系设置为NSManagedObject,而不是另一个实体的类.我尝试重新生成这个类几次,然后将其更改为应该是什么,并将类添加到.h,#import到.m

我假设[MANAGEDOBJECT count]消息是核心数据魔法的一部分,我不会在我的代码中的任何地方调用它.我不知道从哪里开始,该怎么做,我可以在这里发布什么来帮助得到答案?我试过了:

weather.hiveObservation = hiveObs;
Run Code Online (Sandbox Code Playgroud)

以及

[weather setValue:hiveObs forKey:@"hiveObservation"]
Run Code Online (Sandbox Code Playgroud)

两个都抛出相同的错误,如果我注释掉设置对象保存到持久存储的关系.hiveObs是在旧视图中创建的,但尚未保存...如果这很重要?

编辑:经过思考和添加信息.
我传入的ManagedObject已创建,但在更改视图之前未保存.它在新视图中使用nil值记录,但这可能是问题吗?

核心数据文件
HiveObservation.h

import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class BoxObservation, HiveDetails, WeatherObservation;

@interface HiveObservation : NSManagedObject
... //other properties omitted
@property (nonatomic, retain) HiveDetails *hiveDetail;
@property (nonatomic, retain) WeatherObservation *weatherObservation;
@end

@interface HiveObservation (CoreDataGeneratedAccessors)

- (void)addBoxObservationsObject:(BoxObservation *)value;
- (void)removeBoxObservationsObject:(BoxObservation *)value;
- (void)addBoxObservations:(NSSet *)values;
- (void)removeBoxObservations:(NSSet *)values;

@end
Run Code Online (Sandbox Code Playgroud)

WeatherObservation.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class HiveObservation;

@interface WeatherObservation : NSManagedObject
//... other properties omitted
@property (nonatomic, retain) NSNumber * windSpeed;
@property (nonatomic, retain) HiveObservation *hiveObservation;

@end
Run Code Online (Sandbox Code Playgroud)

控制台输出:

2015-01-21 14:28:32.739 Hive Maps[5085:69277] -[WeatherObservation count]: unrecognized selector sent to instance 0x78ea3140
2015-01-21 14:28:32.743 Hive Maps[5085:69277] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WeatherObservation count]: unrecognized selector sent to instance 0x78ea3140'
*** First throw call stack:
(
    0   CoreFoundation                      0x0157b946 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01204a97 objc_exception_throw + 44
    2   CoreFoundation                      0x015835c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x014cc3e7 ___forwarding___ + 1047
    4   CoreFoundation                      0x014cbfae _CF_forwarding_prep_0 + 14
    5   CoreData                            0x00b2a8cc -[NSSQLCore _knownOrderKeyForObject:from:inverseToMany:] + 204
    6   CoreData                            0x00b2ae53 -[NSSQLCore _populateRow:fromObject:timestamp:inserted:] + 1043
    7   CoreData                            0x00a52e73 -[NSSQLCore prepareForSave:] + 1091
    8   CoreData                            0x00a52174 -[NSSQLCore saveChanges:] + 340
    9   CoreData                            0x00a1bfee -[NSSQLCore executeRequest:withContext:error:] + 638
    10  CoreData                            0x00b15af4 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 5380
    11  CoreData                            0x00b1ee9c gutsOfBlockToNSPersistentStoreCoordinatorPerform + 188
    12  libdispatch.dylib                   0x02d00e2f _dispatch_client_callout + 14
    13  libdispatch.dylib                   0x02ce5505 _dispatch_barrier_sync_f_invoke + 144
    14  libdispatch.dylib                   0x02ce4c37 dispatch_barrier_sync_f + 105
    15  CoreData                            0x00b0f7e7 _perform + 183
    16  CoreData                            0x00a1bb1b -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 459
    17  CoreData                            0x00a4b1d9 -[NSManagedObjectContext save:] + 1529
    18  Hive Maps                           0x00018a6d -[GetWeatherTableViewController saveWeatherData:] + 3133
    19  libobjc.A.dylib                     0x0121a7cd -[NSObject performSelector:withObject:withObject:] + 84
    20  UIKit                               0x0193023d -[UIApplication sendAction:to:from:forEvent:] + 99
    21  UIKit                               0x01ca0840 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
    22  libobjc.A.dylib                     0x0121a7cd -[NSObject performSelector:withObject:withObject:] + 84
    23  UIKit                               0x0193023d -[UIApplication sendAction:to:from:forEvent:] + 99
    24  UIKit                               0x019301cf -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    25  UIKit                               0x01a63e86 -[UIControl sendAction:to:forEvent:] + 69
    26  UIKit                               0x01a642a3 -[UIControl _sendActionsForEvents:withEvent:] + 598
    27  UIKit                               0x01a6350d -[UIControl touchesEnded:withEvent:] + 660
    28  UIKit                               0x0198060a -[UIWindow _sendTouchesForEvent:] + 874
    29  UIKit                               0x019810e5 -[UIWindow sendEvent:] + 791
    30  UIKit                               0x01946549 -[UIApplication sendEvent:] + 242
    31  UIKit                               0x0195637e _UIApplicationHandleEventFromQueueEvent + 20690
    32  UIKit                               0x0192ab19 _UIApplicationHandleEventQueue + 2206
    33  CoreFoundation                      0x0149f1df __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    34  CoreFoundation                      0x01494ced __CFRunLoopDoSources0 + 253
    35  CoreFoundation                      0x01494248 __CFRunLoopRun + 952
    36  CoreFoundation                      0x01493bcb CFRunLoopRunSpecific + 443
    37  CoreFoundation                      0x014939fb CFRunLoopRunInMode + 123
    38  GraphicsServices                    0x04d0124f GSEventRunModal + 192
    39  GraphicsServices                    0x04d0108c GSEventRun + 104
    40  UIKit                               0x0192e8b6 UIApplicationMain + 1526
    41  Hive Maps                           0x0001967d main + 141
    42  libdyld.dylib                       0x02d2cac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException  
Run Code Online (Sandbox Code Playgroud)

我做了一个带有异常点的堆栈跟踪,当我保存时发生异常:

weather.hiveObservation = hiveObs;       
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
     //Handle the error.
     NSLog(@"SAVE ERROR: %@",error);
}   
Run Code Online (Sandbox Code Playgroud)

屏幕截图,可以直接发布图像

Ris*_*kov 14

有同样的问题.清理和重建后仍出现错误.双重检查了关系,它确实是一对一,我仍然有同样的错误.

解决方案:删除"hiveObservation"关系和相应实体中的对应关系(如果存在),并添加具有相同名称的新关系.


sta*_*Man 7

我也遇到过这个错误:

-[SomeEntity count]:无法识别的选择器发送到实例 0x600000db2d00

这似乎仅发生在一对一关系中(这似乎是 Xcode 错误)

基本上,错误告诉我们count正在执行SomeEntity,因此我们需要找到该实体在我们的核心数据模型中用作关系的位置。

解决方案:

  1. 前往不当关系
  2. 将其更改typeTo-Many
  3. 取消选中ordered
  4. type回原来的样子To-One

原因:

如果您对核心数据模型进行更改,并且通过仅更改类型但将集合保留为 ,无意中将关系从 an 更改为 a ,则可能会出现这种情况。ordered to-manyto-onearrangementordered

Xcode 在此处出现错误,导致生成的 xml 保留可能决定其运行时行为的ordered属性设置YES,尤其是在保存上下文时。

根据记录,XML 条目如下所示:

  1. 多对多订购(正确):

     <relationship name="foo" toMany="YES" ordered="YES" destinationEntity="SomeEntity" deletionRule="Nullify" syncable="YES"/>
    
    Run Code Online (Sandbox Code Playgroud)
  2. To-One(由于ordered保留属性而会导致本次崩溃的BUG):

     <relationship name="foo" maxCount="1" ordered="YES" destinationEntity="SomeEntity" deletionRule="Nullify" syncable="YES"/>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 对一(正确):

     <relationship name="foo" maxCount="1" destinationEntity="SomeEntity" deletionRule="Nullify" syncable="YES"/>
    
    Run Code Online (Sandbox Code Playgroud)

这与删除关系并用相同的名称重新创建它的作用相同,但我认为有些人可能想知道确切的原因。

PS:我的 git 更改日志帮助我找到了这个。