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

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.


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.

Tags:

Ios

Core Data