0

私は2つの配列を持っています

  1. retrievedNamesMutableArray
  2. 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];

 }
4

1 に答える 1

1

最初のループは、名前を含むが画像を含まないオブジェクトを作成し、2 番目のループは、画像を含むが名前を含まないのオブジェクトを作成します。

(以前の質問から)両方の配列が同じサイズであると仮定すると、名前/画像ごとに1つのオブジェクトのみを作成する必要があります。

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"];
}
于 2013-09-02T14:01:32.560 に答える