Core Data - action before remove an instance of an entity

Discussion in 'Mac Programming' started by kubap007, Jul 21, 2008.

  1. kubap007 macrumors newbie

    Joined:
    Jul 21, 2008
    #1
    Hello!

    I've some problem.
    I've 3 entities: BillOfSale, Product, Item.

    When I remove an instance of an Item entity I want to decrement total field (attribiute in BillOfSale entity). I wrote a custom class: ItemMO where I increment total fileld using reletionship:

    Code:
    [self setValue: sum forKeyPath:@"billOfSale.total"];
    
    I call this function when I change quantity or product and it's working.
    But I don't know what method is called when an instance of an entity is being removed.
    In the interface I bind "Delete Item" button to ItemsController (NSArayController for Item entity) remove action.

    I'm looking for something like awakeFromInsert method but I can't find it.

    Please help me :(
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    I assume the object will always be removed in the UI, then I would create a custom remove method which checked the sum value manually. Then I'd call that method instead of remove on the NSArrayController.

    You'll probably then have to remove the object manually from the NSManagedObjectContext using [context deleteObject:obj]; where obj is the object in question.
     
  3. kubap007 thread starter macrumors newbie

    Joined:
    Jul 21, 2008
    #3
    OK thanks I will try but I don't know how to bind button to method defined by me. Earlier I just copy method implementation from data model and changed it so it wasn't a problem.
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    I assume you have a custom controller... You just control drag from the button to your custom controller.
     
  5. kubap007 thread starter macrumors newbie

    Joined:
    Jul 21, 2008
    #5
    Unfortunately I haven't, I wrote custom classes and select them for chosen entity in data model. Obviously I have 3 controllers one for each entity, but I drag them from pallet and I change only controlled entity. Sorry for my stupid questions but I just started with cocoa and I must learn a lot.

    I was searching how to create custom controller and I probably know how. I should create NSArrayController subclass, but I still don't know how to override remove method . I create files for new subclass and write method - (void)removeObject:(id)object but I don't know how I can change total field or even delete object. When I override for example method -(void)setQuantity: (short)value I used keyword self and path forKeyPath:mad:"billOfSale.total"
     
  6. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    You don't want to subclass NSArrayController. You will basically always need a standard Cocoa controller for something (especially once you do anything complex), and this is something you'll need one for.

    If you want to check out the idea you could take a look at my source code.
     
  7. kubap007 thread starter macrumors newbie

    Joined:
    Jul 21, 2008
    #7
    Thanks a lot I will see your code and I hope I will understand :)

    How can I get object form (id)sender.

    - (void) remove: (id)sender
    {
    NSManagedObject *obj = [??? sender];
    ...
    }

    Or do I have to do something else?
     
  8. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    ^^ My code may be hard to understand as its pretty advanced, but it is Core Data.

    To get the object you want to do

    NSManagedObject *obj=[[arrayController selectedObjects] objectAtIndex:0];

    Where arrayControlller is an IBOutlet from the custom controller (in the header file at the top section) to the NSArrayController in Interface builder.
     
  9. kubap007 thread starter macrumors newbie

    Joined:
    Jul 21, 2008
    #9
    Thank you for your help! Everything works.
    Have a nice day :)
     

Share This Page