Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

core-data

WeatherObservation is an entity in my data model, I'm trying to set it's relationship to another Object (one to one), which I passed in through the navigation controller. It worked a few weeks ago. I changed some of the other entities in my model, regenerated the classes and this relationship was set to NSManagedObject, instead of the class for the other entity. I tried re-generating this class a few times, then just changed it to what it should be and added the class to .h, #import to .m

I'm assuming the [MANAGEDOBJECT count] message is part of the core data magic, I don't call it anywhere in my code. I'm not sure where to start, what to do, what I could post here to help get an answer? I've tried:

weather.hiveObservation = hiveObs;

as well as

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

both throw the same error, if I comment out setting the relationship the object is saved to the persistent store. hiveObs is created in the old view, but hasn't been saved yet... if that matters?

EDIT: After thought and Added info.
The ManagedObject I'm passing in was created, but not saved before changing views. It logs with nil values in the new view, but could that be the issue?

Core Data Files
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

Console Output:

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  

I did a stack-trace with an exception point inputed, the exception occurs when I save:

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

Screen shot, can post the image directly

like image 971
Kevin Avatar asked Jan 21 '15 21:01

Kevin


2 Answers

Had the same problem. Error occurred even after clean and rebuild. Double checked the relation and it was indeed One to One and I still had the same error.

The solution: delete the "hiveObservation" relation and his counterpart in the corresponding Entity (if present) and them add new ones with the same names.

like image 74
Riskov Avatar answered Nov 10 '22 01:11

Riskov


I too came across this error:

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

This seems to happen with to-one relationships only (it appears to be an Xcode bug)

Basically the error is telling us that count is being performed on SomeEntity, so we need to locate where this entity is being used as a relationship with-in our Core-Data model.

Solution:

  1. Go to the misbehaving relationship
  2. Change it's type to To-Many
  3. Uncheck ordered
  4. Change it's type back to To-One

Reason:

This scenario might occur if you're making changes to the core data model and innocently change a relationship from an ordered to-many to a to-one by changing only the type but leaving the arrangement set as ordered.

Xcode bugs out here and causes the generated xml to keep the ordered attribute set to YES which probably is determining it's runtime behavior later on, especially when saving the context.

For the record this is what the XML entries would look like:

  1. To-Many Ordered (Proper):

     <relationship name="foo" toMany="YES" ordered="YES" destinationEntity="SomeEntity" deletionRule="Nullify" syncable="YES"/>
    
  2. To-One (the BUG that will cause this crash due to the ordered attribute being kept):

     <relationship name="foo" maxCount="1" ordered="YES" destinationEntity="SomeEntity" deletionRule="Nullify" syncable="YES"/>
    
  3. To-One (Proper):

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

This works the same as deleting the relationship and recreating it with the same name but I thought some people out there might like to know the exact cause.

PS: My git change log helped me locate this.

like image 32
staticVoidMan Avatar answered Nov 10 '22 02:11

staticVoidMan