Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set default values on new properties for existing entities after light weight core data migration?

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.

like image 372
Moritz Avatar asked May 03 '11 09:05

Moritz


People also ask

What property do you use to set the default value?

Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.

How do I change the default value in XCode?

Open the Utilities panel (CMD + ALT + 0) (the one from the right side in XCode), then click on the third option (Show the Data Model inspector). You'll find there all you need. You must select the attribute you want to set the default value for. Omg I did not know this, has always been doing this in code.

What is migration in Coredata?

Core Data can typically perform an automatic data migration, referred to as lightweight migration. Lightweight migration infers the migration from the differences between the source and the destination managed object models.

What is core data in iOS Swift?

Getting Started Core Data (CRUD) with Swift. Core Data is a graphical and persistence framework, which is used in Apple devices with operating systems macOS and iOS. Core Data was first introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0.

How are configuration values generated in EF Core?

Thank you. Database columns can have their values generated in various ways: primary key columns are frequently auto-incrementing integers, other columns have default or computed values, etc. This page details various patterns for configuration value generation with EF Core.

How can I configure a property to have its value generated?

You can configure any property to have its value generated for inserted entities as follows: Similarly, a property can be configured to have its value generated on add or update: Unlike with default values or computed columns, we are not specifying how the values are to be generated; that depends on the database provider being used.

How do I change the default value of a form?

Set a default value In the Navigation Pane, right-click the form that you want to change, and then click Design View. Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value.

What happens when you set a default value for a field?

If you set a default value for a table field, Access applies your value to any controls that you base on that field. If you don't bind a control to a table field, or you link to data in other tables, you set a default value for your form controls itself.


1 Answers

I finally figured it out after 6 months.

The attributes that are added to a core data entity have to be marked as non-optional. Only then will the default values be set automatically during lightweight migration for the entities that were created with the old data model.

like image 93
Moritz Avatar answered Oct 06 '22 00:10

Moritz