简体   繁体   中英

How to save and retrieve two arrays in Core Data in iOS6

I have two arrays

  1. retrievedNamesMutableArray
  2. retrievedImagesArray

which I am saving in Core Data. Although the saving operation is successful, when I fetch data, it seems to fetch either Names or Images but not both. I assume I can store a NSDictionary in Core Data but can't seem to figure out a way to do it.

This is what I am doing to save to 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];
}

This is how I fetch.

-(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];

 }

Your first loop creates objects containing a name but no image, and your second loop create different objects containing an image but no name.

Assuming (from your previous questions) that both arrays have the same size, you should create only one object for each name/image:

for (NSUInteger i = 0; i < [self.reterivedNamesMutableArray count]; i++) {
    NSString *object = self.reterivedNamesMutableArray[i];
    UIImage *img = self.reterivedImagesArray[i];

    newContact = [NSEntityDescription insertNewObjectForEntityForName:@"AddressBook" inManagedObjectContext:context];
    [newContact setValue:@"GroupOne" forKey:@"groups"];
    [newContact setValue:object forKey:@"firstName"];
    [newContact setValue:img forKey:@"photo"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM