简体   繁体   English

如何在iOS6的Core Data中保存和检索两个数组

[英]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. 我假设我可以在核心数据中存储NSDictionary,但似乎无法找出一种实现方法。

This is what I am doing to save to Core Data. 这就是我要保存到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"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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