私は2つの配列を持っています
retrievedNamesMutableArray
retrievedImagesArray
Core Dataに保存しています。保存操作は成功しましたが、データを取得すると、名前または画像のいずれかが取得され、両方が取得されないようです。Core Data に NSDictionary を格納できると思いますが、それを行う方法がわかりません。
これは、Core Data に保存するために行っていることです。
-(void)saveToPhoneDatabase
{
AddressBookAppDelegate *appDelegate =[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
/* I assume this can be done but can't figure out proper process.
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setObject:self.retrievedNamesMutableArray forKey:@"NamesArray"];
[dictionary setObject:self.retrievedImagesArray forKey:@"ImagesArray"];
*/
for (NSString *object in self.retrievedNamesMutableArray)
{
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"AddressBook" inManagedObjectContext:context];
[newContact setValue:@"GroupOne" forKey:@"groups"];
[newContact setValue:object forKey:@"firstName"];
}
for (UIImage *img in self.retrievedImagesArray)
{
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"AddressBook" inManagedObjectContext:context];
[newContact setValue:img forKey:@"photo"];
NSLog(@"Saved the photos of Array");
}
[context save:nil];
}
これが私が取得する方法です。
-(void)fetchFromPhoneDatabase
{
AddressBookAppDelegate *appDelegate =[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"AddressBook" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSError *error;
self.arrayForTable = [context executeFetchRequest:request error:&error];
NSLog(@"contents from core data = %@",self.arrayForTable);
[self.tableView reloadData];
}