IOS CoreData + MoGenerator: how to initialize a managed object once only when I use nested contexts?

I am using mogenerator to generate code from a TestPerson managed object model. TestPerson inherits from the abstract TLSyncParent object. In TLSyncParent, I have the code:

- (void) awakeFromInsert
{
    [super awakeFromInsert];
    QNSLOG(@"%@\n%@", self.managedObjectContext, self.description);
    if (self.syncStatus == nil) {
        self.syncStatusValue = SYNCSTATUS_NEW;
        self.tempObjectPID = [self generateUUID];
        QNSLOG(@"After init values\n%@", self.description);
    }
}

I am creating a TestPerson object in childMOC whose parent is mainMOC whose parent is rootMOC. awakeFromInsert works as expected and makes init changes. When I save childMOC in mainMOC, awakeFromInsert starts again. From the documents, I would not expect this, but there is some ambiguity. From Documents "Usually this method is used to initialize special property values ​​by default. This method is called only once in the life of the object." The real problem is that when awakeFromInsert runs in mainMOC, the init changes made to childMOC are NOT. awakeFromInsert seems to be executed before actually saving occurs.

2013-10-02 11:22:45.510_xctest[21631:303] TestPerson -awakeFromInsert <NSManagedObjectContext: 0xd684780>
<TestPerson: 0xd6863b0> (entity: TestPerson; id: 0xd684ed0 <x-coredata:///TestPerson/t02B71E0D-AE3F-4605-8AC7-638AE072F2302> ; data: {
    dept = nil;
    job = nil;
    objectPID = nil;
    personName = nil;
    syncStatus = 0;
    tempObjectPID = nil;
    updatedAt = nil;
})
2013-10-02 11:22:45.511_xctest[21631:303] TestPerson -awakeFromInsert After init values
<TestPerson: 0xd6863b0> (entity: TestPerson; id: 0xd684ed0 <x-coredata:///TestPerson/t02B71E0D-AE3F-4605-8AC7-638AE072F2302> ; data: {
    dept = nil;
    job = nil;
    objectPID = nil;
    personName = nil;
    syncStatus = 4;
    tempObjectPID = "7AB46623-C597-4167-B189-E3AAD24954DE";
    updatedAt = nil;
})
2013-10-02 11:22:45.511_xctest[21631:303] CoreDataController -saveChildContext: Saving Child MOC
2013-10-02 11:22:45.511_xctest[21631:303] TestPerson -awakeFromInsert <NSManagedObjectContext: 0xd682180>
<TestPerson: 0xd68fce0> (entity: TestPerson; id: 0xd684ed0 <x-coredata:///TestPerson/t02B71E0D-AE3F-4605-8AC7-638AE072F2302> ; data: {
    dept = nil;
    job = nil;
    objectPID = nil;
    personName = nil;
    syncStatus = 0;
    tempObjectPID = nil;
    updatedAt = nil;
})
2013-10-02 11:22:45.511_xctest[21631:303] TestPerson -awakeFromInsert After init values
<TestPerson: 0xd68fce0> (entity: TestPerson; id: 0xd684ed0 <x-coredata:///TestPerson/t02B71E0D-AE3F-4605-8AC7-638AE072F2302> ; data: {
    dept = nil;
    job = nil;
    objectPID = nil;
    personName = nil;
    syncStatus = 4;
    tempObjectPID = "B799AFDA-3514-445F-BB6F-E4FE836C4F9D";
    updatedAt = nil;
})

What is the appropriate place to initialize a managed entity when using the MoGenerator structure?

0
3

, , . , , , . MoGenerator. NSManagedObject initWithMOC. awakeFromCreate . awakeFromCreate , awakeFromInsert. , MO initWithMOC.

@implementation NSManagedObject (CoreDataController)

+ (NSManagedObject*) initWithMOC: (NSManagedObjectContext*) context
{
    NSManagedObject* mo = (NSManagedObject*)
            [NSEntityDescription insertNewObjectForEntityForName: NSStringFromClass(self)
                                          inManagedObjectContext: context];

    [mo awakeFromCreate];
    return mo;
}

- (void) awakeFromCreate
{
    return;
}
-3

awakeFromInsert . ,

Core, .

- ".. ", ( ) . , awakeFromInsert . - .

. - , - :

if ([[self managedObjectContext] parentContext] != nil) {
    // Set default values here
}

, - . - , . , - , , "" . , , .

( , ) , , awakeFromInsert. , setDefaultValues, syncStatusValue tempObjectPID. . , , , , .

+4

I am sure that Mogenerator does not change the way of creating managed objects, but only moves the actual classes of managed objects to the files generated by the machine with the "_" prefix and subclasses these managed objects to place all your user logic so that it does not get lost during recovery classes of managed objects.

+1
source

All Articles