Sunday 1 September 2013

How to save contents of Array in CoreData in iOS 6

How to save contents of Array in CoreData in iOS 6

I am fetching the contents of AddressBook which in turn are copied into an
array.Now i want to save this array into CoreData.I know how to insert
single values in CoreData.How do i loop through an array to do the same?
Here is what i tried.
-(void)saveToDatabase
{
AddressBookAppDelegate *appDelegate =[[UIApplication
sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription
insertNewObjectForEntityForName:@"AddressBook"
inManagedObjectContext:context];
[newContact setValue:@"GroupOne" forKey:@"groups"];
NSLog(@"%@",[newContact valueForKey:@"groups"]); //this works fine.
for (NSString *object in self.reterivedNamesMutableArray) // this array
holds the name of contacts which i want to insert into CoreData.
{
[newContact setValue:object forKey:@"firstName"];
NSLog(@"Saved the contents of Array");
}
[context save:nil];
NSLog(@"Saved firstName = %@",[newContact valueForKey:@"firstName"]);
// shows null
NSLog(@"Saved groups = %@",[newContact valueForKey:@"groups"]); //
works fine.
}

No comments:

Post a Comment