I’ve added a new entity (Site) to my Core Data Model, and a relationship to my old entity (Notification) pointing at Site. What I need to do is populate the new entity with a single object (the values for the attributes of the new object are in User Defaults right now), and then assign all of the old objects to the variable representing the relationship.
The notifications variable on the Site object is one-to-many and optional. The site variable on the Notification object is one-to-one and required.
My new object looks like so:
#import <Foundation/Foundation.h>
@interface Site : NSManagedObject
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) NSString *accessKey;
@property (nonatomic, retain) NSString *secretKey;
@property (nonatomic, retain) NSSet *notifications;
@end
The old object has a new ‘site’ attribute:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Site.h"
@interface Notification : NSManagedObject
@property (nonatomic, retain) NSNumber *created;
@property (nonatomic, retain) NSNumber *isRead;
@property (nonatomic, retain) NSString *message;
@property (nonatomic, retain) NSNumber *notificationId;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) NSString *urlMarkRead;
@property (nonatomic, retain) Site *site;
@end
Any ideas how I can accomplish this?
You can do a manual migration. It’s going to be some work for you to code that up, but it’ll solve your problem.
Take a look at the Core Data Versioning and Data Migration Programming Guide. There you’ll find The Migration Process with a subsection called Custom Entity Migration Policies which describes your situation.
You need to create a subclass of
NSEntityMigrationPolicy.