0

I have two versioned Data Models in my iOS project.

The difference between the Data Models is that newer model has a new field named "new_col".

In some part of the project, I need to fetch data filtered by this "new_col" field.

So, I used the following codes.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"new_col = %@", val];
[request setPredicate:predicate];

NSArray *results = [context executeFetchRequest:request error:&err];

And I faced the exception - 'NSInvalidArgumentException', reason: 'keypath new_col not found in entity ...

I know that it's because of the original data model.

I think that I should convert the original data to the new data model.

How can I convert?

Yun
  • 5,233
  • 4
  • 21
  • 38

2 Answers2

0

Quick Google.

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/Introduction.html

This is supported by Apple and is the correct way to migrate your users data from one model to another.

Tim
  • 8,932
  • 4
  • 43
  • 64
0

If you are adding new attributes and want to maintain backwards compatibility you should specify a default value for the new attribute, or leave it as "optional" in the core data editor.

Once you have done this, you need to make sure automatic migration is enabled. See Implementation of “Automatic Lightweight Migration” for Core Data for details on how to do this. Existing/old entities will then take on the default value (if defined) or nil if the attribute is optional.

Community
  • 1
  • 1
Mike Weller
  • 45,401
  • 15
  • 131
  • 151