[Image count]: unrecognized selector sent to instance
Asked Answered
A

2

5

I get the following error when saving managedObjectContext:

Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[Image count]: unrecognized selector sent to instance

Code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {

// Delete any existing image.
Image *oldImage = _place.image;
if (oldImage != nil) {
    [_place.managedObjectContext deleteObject:oldImage];
}

Image *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:_place.managedObjectContext];
image.imageData = UIImageJPEGRepresentation(selectedImage, 0.5);
_place.image = image;
[_place.managedObjectContext save:nil]; //Here is the crash
[self dismissViewControllerAnimated:YES completion:nil];

Image entity has two one-to-one relationships: place and trip.

@interface Image : NSManagedObject

@property (nonatomic, retain) NSData * imageData;
@property (nonatomic, retain) Trip *trip;
@property (nonatomic, retain) Place *place;

@end

Can't understand why [Image count] error comes out on one-to-one relationship.

Upd.:

@interface Place : NSManagedObject

@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) id thumbnail;

@property (nonatomic, retain) Image *image;

@end

Upd.2:

*** First throw call stack:
(
    0   CoreFoundation                      0x022935e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01cef8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x02330903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0228390b ___forwarding___ + 1019
    4   CoreFoundation                      0x022834ee _CF_forwarding_prep_0 + 14
    5   CoreData                            0x00803df8 -[NSSQLCore _knownOrderKeyForObject:from:inverseToMany:] + 200
    6   CoreData                            0x0073ad50 -[NSSQLCore _populateRowForOp:withObject:] + 1120
    7   CoreData                            0x00750e27 -[NSSQLCore recordValuesForInsertedObject:] + 71
    8   CoreData                            0x00739b5d -[NSSQLCore recordChangesInContext:] + 685
    9   CoreData                            0x00738925 -[NSSQLCore saveChanges:] + 565
    10  CoreData                            0x0070555c -[NSSQLCore executeRequest:withContext:error:] + 412
    11  CoreData                            0x00705050 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 4704
    12  CoreData                            0x00731ccc -[NSManagedObjectContext save:] + 764
    13  Travel Plan                         0x0001cf12 -[PlaceViewController imagePickerController:didFinishPickingImage:editingInfo:] + 738
    14  UIKit                               0x00cd5e6c -[UIImagePickerController _imagePickerDidCompleteWithInfo:] + 488
    15  PhotoLibrary                        0x13694e94 PLNotifyImagePickerOfImageAvailability + 106
    16  PhotosUI                            0x14411585 -[PUUIPhotosAlbumViewController handleNavigateToAsset:inContainer:] + 401
    17  PhotosUI                            0x143947b4 -[PUPhotosGridViewController collectionView:shouldSelectItemAtIndexPath:] + 577
    18  UIKit                               0x0106dc0b -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 173
    19  UIKit                               0x010861f8 -[UICollectionView _userSelectItemAtIndexPath:] + 189
    20  UIKit                               0x010863b5 -[UICollectionView touchesEnded:withEvent:] + 437
    21  libobjc.A.dylib                     0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
    22  UIKit                               0x00bb6902 forwardTouchMethod + 271
    23  UIKit                               0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
    24  libobjc.A.dylib                     0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
    25  UIKit                               0x00bb6902 forwardTouchMethod + 271
    26  UIKit                               0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
    27  libobjc.A.dylib                     0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
    28  UIKit                               0x00bb6902 forwardTouchMethod + 271
    29  UIKit                               0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
    30  UIKit                               0x00dd1c7f _UIGestureRecognizerUpdate + 7166
    31  UIKit                               0x00a9c19a -[UIWindow _sendGesturesForEvent:] + 1291
    32  UIKit                               0x00a9d0ba -[UIWindow sendEvent:] + 1030
    33  UIKit                               0x00a70e86 -[UIApplication sendEvent:] + 242
    34  UIKit                               0x00a5b18f _UIApplicationHandleEventQueue + 11421
    35  CoreFoundation                      0x0221c83f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    36  CoreFoundation                      0x0221c1cb __CFRunLoopDoSources0 + 235
    37  CoreFoundation                      0x0223929e __CFRunLoopRun + 910
    38  CoreFoundation                      0x02238ac3 CFRunLoopRunSpecific + 467
    39  CoreFoundation                      0x022388db CFRunLoopRunInMode + 123
    40  GraphicsServices                    0x046299e2 GSEventRunModal + 192
    41  GraphicsServices                    0x04629809 GSEventRun + 104
    42  UIKit                               0x00a5dd3b UIApplicationMain + 1225
    43  Travel Plan                         0x0004c53d main + 141
    44  libdyld.dylib                       0x02dd170d start + 1
)
Abirritate answered 17/12, 2013 at 12:45 Comment(9)
please show definition of place as wellFugato
@Fugato I will add it right now.Abirritate
If you put a breakpoint on exceptions, what does the trace look like that ends up at [Image count]?Sonnnie
@PhillipMills I updated the post, please take a look.Abirritate
That inverseToMany: certainly looks suspicious. Any chance that there used to be a to-many relationship that you've removed from code but still persists in the model? Or that some cached piece of the model hasn't been cleaned? (I know it's a long shot, but I'd try a complete "clean" and "delete derived data" to see if something was out of synch.)Sonnnie
and the relevant entity definitions from the MOM? It looks like CD thinks it should a set of images or somethingFugato
@Fugato It's one-to-one in both place and image. Can't understand why this happens.Abirritate
@PhillipMills I checked it 3 times, but the model is OK. I cleaned and removed derived data, but this doesn't help.Abirritate
@PhillipMills I set place.image relationship in Xcode to many, and then back to one. Now it works, thanks!Abirritate
A
8

That was some bug in Xcode. After setting relationship to many, and then back to one, it started to work without errors.

Abirritate answered 17/12, 2013 at 14:23 Comment(4)
I'm also had such a problem. It was solved by deleting "broken" entity from model (including all relationships), building project and then creating it from scratch.Friction
Médéric Petit's answer is much faster and more effective than zakhej's. I'd recommend trying that first, it worked like a charm for me.Aube
@pallzoltan He wrote exactly the same as me and almost 6 months later.Abirritate
@Shmidt, he mentioned unticking the Ordered checkbox, which makes the difference. I've done what you've suggested and it didnt work until I unticked the Ordered checkbox ;)Aube
S
20

I got exactly the same problem after changing some relations in my models. This seems to be a bug in Xcode that appears when you change a relationship that was "to many : ordered" to a "to one" relation. It's like Xcode still thinks that the relation is to many because the "ordered" tick-box is ticked.

A very easy solution to solve this is to change back to a "to many" relationship, unselect "ordered" and then change it back again to a "to one" relation.

Susie answered 19/5, 2014 at 10:19 Comment(2)
You are a saviour lol :) I've been checking this for ages now.Vessel
this bug still exists and this solution still worksForborne
A
8

That was some bug in Xcode. After setting relationship to many, and then back to one, it started to work without errors.

Abirritate answered 17/12, 2013 at 14:23 Comment(4)
I'm also had such a problem. It was solved by deleting "broken" entity from model (including all relationships), building project and then creating it from scratch.Friction
Médéric Petit's answer is much faster and more effective than zakhej's. I'd recommend trying that first, it worked like a charm for me.Aube
@pallzoltan He wrote exactly the same as me and almost 6 months later.Abirritate
@Shmidt, he mentioned unticking the Ordered checkbox, which makes the difference. I've done what you've suggested and it didnt work until I unticked the Ordered checkbox ;)Aube

© 2022 - 2024 — McMap. All rights reserved.