How do I set default values on new properties for existing entities after light weight core data migration?
Posted
by
Moritz
on Stack Overflow
See other posts from Stack Overflow
or by Moritz
Published on 2011-05-03T09:07:59Z
Indexed on
2012/07/11
9:15 UTC
Read the original article
Hit count: 284
I've successfully completed light weight migration on my core data model.
My custom entity Vehicle received a new property 'tirePressure' which is an optional property of type double with the default value 0.00.
When 'old' Vehicles are fetched from the store (Vehicles that were created before the migration took place) the value for their 'tirePressure' property is nil. (Is that expected behavior?)
So I thought: "No problem, I'll just do this in the Vehicle class:"
- (void)awakeFromFetch {
[super awakeFromFetch];
if (nil == self.tirePressure) {
[self willChangeValueForKey:@"tirePressure"];
self.tirePressure = [NSNumber numberWithDouble:0.0];
[self didChangeValueForKey:@"tirePressure"];
}
}
Since "change processing is explicitly disabled around" awakeFromFetch I thought the calls to willChangeValueForKey and didChangeValueForKey would mark 'tirePresure' as dirty.
But they don't.
Every time these Vehicles are fetched from the store 'tirePressure' continues to be nil despite having saved the context.
© Stack Overflow or respective owner