views:

35

answers:

2

Trying to update an fetched object and getting 'unrecognized selector sent to instance' error whenever I try and set any property on the object, here is my code:

NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entities = [NSEntityDescription entityForName:@"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];
[request setEntity:entities];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ItemSKU=%@",collectionItem.ItemSKU];
[request setPredicate:predicate];

NSArray *results= [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    if ([results count]==0) {
     /* add another purchase order item */
    PurchaseOrderItem *newPOItem = (PurchaseOrderItem*) [NSEntityDescription insertNewObjectForEntityForName:@"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];
    [newPOItem setProductName:productName.text];
    [newPOItem setPrice:price];
    [newPOItem setQuantity:qty];
    [newPOItem setItemSKU:ItemSKU];
    [newPOItem setSubTotal:itemSubTotal];   
        }else {
    /* update item */
    PurchaseOrderItem *updateObject = (PurchaseOrderItem*)[results objectAtIndex:0];
    [updateObject setSubTotal:itemSubTotal];
    [updateObject setQuantity:qty];
        }

EDIT: here is the exact error:

2010-07-12 22:07:37.920 RCoreData[46733:207] *** -[PurchaseOrder setSubTotal:]: unrecognized selector sent to instance 0x587b270
2010-07-12 22:07:37.921 RCoreData[46733:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[PurchaseOrder setSubTotal:]: unrecognized selector sent to instance 0x587b270'
2010-07-12 22:07:37.921 RCoreData[46733:207] Stack: (
    46504016,
    47661868,
    46512731,
    45975158,
    45971954,
    209089,
    3382510,
    3879998,
    3889344,
    3884141,
    3509736,
    3401283,
    3432920,
    53940604,
    45783196,
    45779112,
    53934237,
    53934434,
    3425138,
    10940,
    10794
)
terminate called after throwing an instance of 'NSException'

Note: Adding the item is working great, it is the update part that is throwing the error:

/* update item */
  PurchaseOrderItem *updateObject = (PurchaseOrderItem*)[results objectAtIndex:0];
  [updateObject setSubTotal:itemSubTotal];
  [updateObject setQuantity:qty];
+1  A: 
PurchaseOrderItem *newPOItem = (PurchaseOrderItem*) [NSEntityDescription insertNewObjectForEntityForName:@"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];

I think this line having problem. Are u sure that your newPOItem is in the correct class, are you sure that casting works well. Can you try with some code like: [newPOItem isKindOfClass:] and [newPOItem respondsToSelector:]

vodkhang
It's not the newPOI item that is the problem, it is updating the old one
Slee
So, check for the old one, I think that is the same problem, something wrong with casting. Are you sure results always contain PurchasesOrderItem?
vodkhang
holy crap, that's what I get for working too long and not getting sleep! I was pulling the object from the wrong fetch results set!
Slee
A: 

First, the cast is completely unnecessary. Both the -insertNewObjectForEntityForName:inManagedObjectContext: and -objectAtIndex: return id which is a generic object. The cast does nothing.

Second, when you run this in the debugger, what do you get when you

po 0x587b270

That will tell you what object it is trying to send the message to.

Third, you are not checking the error. You should always check the error before you start manipulating the array returned.

Marcus S. Zarra