Search code examples
objective-cioscore-datansmanagedobjectobjective-c-category

Core Data: Where to put awakeFromFetch and awakeFromInsert?


I would like to override awakeFromFetch and awakeFromInsert.

As I leave the auto-generated NSManagedObject subclasses unchanged and put my custom code in categories, my question is:

Where do I put awakeFromFetch and awakeFromInsert in order that these methods get called correctly?


Solution

  • If your managed object subclass files are generated by Xcode, then you can also put the methods in a category of the managed object subclass, so that the code is not overwritten when you re-generate the class files in Xcode.

    MyEntity+Extensions.h

    #import "MyEntity.h"
    
    @interface MyEntity (Extensions)
    @end
    

    MyEntity+Extensions.m

    #import "MyEntity+Extensions.h"
    
    @implementation MyEntity (Extensions)
    
    - (void)awakeFromFetch
    {
    
    } 
    
    - (void)awakeFromInsert
    {
    
    }
    @end