CoreData 设置关系给出错误 '-[WeatherObservation count]: unrecognized selector sent to instance

CoreData setting relationship gives error '-[WeatherObservation count]: unrecognized selector sent to instance

WeatherObservation 是我的数据模型中的一个实体,我试图设置它与另一个对象的关系(一对一),我通过导航控制器传入该对象。几周前它起作用了。我更改了我的模型中的其他一些实体,重新生成了 classes 并将此关系设置为 NSManagedObject,而不是另一个实体的 class。我尝试重新生成此 class 几次,然后将其更改为应有的内容并将 class 添加到 .h,#import 到 .m

我假设 [MANAGEDOBJECT count] 消息是核心数据魔术的一部分,我不会在我的代码中的任何地方调用它。我不确定从哪里开始,做什么,我可以 post 在这里帮助获得答案? 我试过:

weather.hiveObservation = hiveObs;

以及

[weather setValue:hiveObs forKey:@"hiveObservation"]

两者都抛出相同的错误,如果我注释掉设置对象保存到持久存储的关系。 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

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

控制台输出:

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  

我做了一个堆栈跟踪,输入了一个异常点,当我保存时出现异常:

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

Screen shot, can post the image directly

有同样的问题。即使在清理和重建之后也会发生错误。 仔细检查了关系,确实是一对一,我仍然有同样的错误。

解决方案:删除 "hiveObservation" 关系及其在相应实体(如果存在)中的对应关系,并添加新的同名关系。

我也遇到了这个错误:

-[SomeEntity count]: unrecognized selector sent to instance 0x600000db2d00

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

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

解决方案:

  1. 转到行为不端的关系
  2. type 更改为 To-Many
  3. 取消选中 ordered
  4. type 改回 To-One

原因:

如果您正在更改核心数据模型并无意中更改了 ordered [=21 的关系,则可能会发生这种情况=]to-one 通过仅更改 type 但保留 arrangement设置为ordered.

Xcode 在这里出现错误并导致生成的 xml 将 ordered 属性设置为 YES 这可能决定了它稍后的运行时行为,尤其是当保存上下文。

郑重声明,XML 个条目应该是这样的:

  1. 对多订购(正确):

     <relationship name="foo" toMany="YES" ordered="YES" destinationEntity="SomeEntity" deletionRule="Nullify" syncable="YES"/>
    
  2. 对一(因保留ordered属性而导致本次崩溃的BUG):

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

     <relationship name="foo" maxCount="1" destinationEntity="SomeEntity" deletionRule="Nullify" syncable="YES"/>
    

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

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