awakeFromInsert called twice with nested contexts

后端 未结 3 723
不思量自难忘°
不思量自难忘° 2021-01-24 07:39

This project uses Mogenerator and Magical Record. I have tracked down a bug to the fact that awakeFromInsert is getting called twice. Once for each of my contexts I

3条回答
  •  天涯浪人
    2021-01-24 08:23

    Open to much better solutions!

    I wish! Would have been easy enough for apple to not invoke awakeFromInsert, or to at least provide a flag that's true in the context of a "parentProcessSaveRequest." If you look at the call stack for the not-first calls to awakeFromInsert, the stack always contains parentProcessSaveRequest.

    Here's some terrible code proving as much:

    - (void) awakeFromInsert 
    {
        [super awakeFromInsert];
    
        NSArray* stackArray = [NSThread callStackSymbols];
        for (NSString* method in stackArray)
        {
            if ([method rangeOfString:@"_parentProcessSaveRequest"].location != NSNotFound)
            {
                NSLog(@"Parent insert %@",self.objectID);
                return;
            }        
        }
        NSLog(@"First insert %@",self.objectID);
        // Initialize here
    
    }
    

    And the log output -- the objectId stays the same:

    2014-05-19 20:53:52.964 myApp[1891:a01f] First insert 0x6000000326c0 
    2014-05-19 20:53:53.531 myApp[1891:303] Parent insert 0xdca8000eb 
    2014-05-19 20:53:53.537 myApp[1891:303] Parent insert 0xdca8000eb 
    

    Seems to work for saving to any of the nested contexts that I have, ugly as it is.

    Unfortunately I cant figure any reasonable way to determine whether awakeFromInsert is being called in the context of a parentProcessSaveRequest. Come on, Apple! Give us a flag here.

提交回复
热议问题