Reputation: 1723
I have already done core data migration to access all data that i had in the prior version of my application.Have an entity named coupon and have several attributes for that entity.But in my new version,i have one more attribute ,"username" for the same entity.Now at the launch of app,i have to fetch all those datas that i had in my older version and have to save all with a username.How can i update my data?Any suggestion will be appreciated.In this version i am always fetching data based on the username.
` NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity: [NSEntityDescription entityForName:@"Coupon" inManagedObjectContext: context]];
NSPredicate *newPredicate= [NSPredicate predicateWithFormat:@"username = %@", username];
[request setPredicate:newPredicate];
NSArray *objects = [context executeFetchRequest:request error:&error];
NSLog(@"%@arry",objects);
NSUInteger count = [context countForFetchRequest: request error: &error];
[request release];
if (count ==0) {
}
` At present,the app crashes here since it does not have such an attribute.So i have to add that attribute before setting this predicate.Anymore details needed for a better solution?
Upvotes: 1
Views: 76
Reputation: 80271
In application:didFinishLaunchingWithOptions:
you can check for the migration. If this is the first time the app is started after upgrading with the new data model, you can first modify all records by adding the username attribute.
//first fetch all Cupons
for (Coupon *c in fetchedObjects) {
c.username = whatever;
}
[self.managedObjectContext save:nil];
Upvotes: 1