How to update an NSManagedObject whenever a specific attribute is changed?

后端 未结 1 1650
伪装坚强ぢ
伪装坚强ぢ 2021-01-23 23:02

Imagine I have a Core Data object, Product. Each Product has a quantity, price, and total attribute. Total is there for efficiency when re

相关标签:
1条回答
  • 2021-01-23 23:47

    You should override the setter method for the quantity attribute of your entity:

    - (void)setQuantity:(NSNumber *)quantity
    {
        [self willChangeValueForKey:@"quantity"];
        [self setPrimitiveValue:quantity forKey:@"quantity"];
        [self didChangeValueForKey:@"quantity"];
    
        NSNumber *price = ... // compute new price
        self.price = price;
    }
    

    You can add that code to a category of the Product class if you don't want to change the Xcode generated files.

    0 讨论(0)
提交回复
热议问题