awakeFromInsert called twice with nested contexts

后端 未结 3 718
不思量自难忘°
不思量自难忘° 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:19

    here is the simplest one: when parentContext is null, means when this context is saved you can do you custom logic, for example incrementing table number

    - (void)awakeFromInsert
     {
    
         if (!self.managedObjectContext.parentContext) {
             //setting tableNumber
    
             [self willChangeValueForKey:@"number"];
             [self setPrimitiveNumber:tableNumber];
             [self didChangeValueForKey:@"number"];
        }
    
     }
    

提交回复
热议问题