0

I'm trying to add images into core data and load it when needed. I'm currently adding the NSImage to the core data as follows:

Thumbnail *testEntity = (Thumbnail *)[NSEntityDescription insertNewObjectForEntityForName:@"Thumbnail" inManagedObjectContext:self.managedObjectContext];
NSImage *image = rangeImageView.image;
testEntity.fileName = @"test";
testEntity.image = image;
NSError *error;
[self.managedObjectContext save:&error];

Thumbnail is the entity name and I have two attributes under the Thumbnail entity - fileName(NSString) and image (id - transformable).

I'm trying to retreive them as below:

NSManagedObjectContext *context = [self managedObjectContext];

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *imageEntity = [NSEntityDescription entityForName:@"Thumbnail" inManagedObjectContext:[context valueForKey:@"image"]];

[fetchRequest setEntity:imageEntity];

NSError *error;

NSArray * array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

if (array == nil) {

    NSLog(@"Testing: No results found");

}else {



   _coreDataImageView.image = [array objectAtIndex:0];
}

I end up with this error:

     [<NSManagedObjectContext 0x103979f60> valueForUndefinedKey:]: this class is not key value coding-compliant for the key image.

The image is added but couldn't retrieve.
Any idea on how to go about with this? Am I doing it right ?

DesperateLearner
  • 1,115
  • 3
  • 19
  • 45

1 Answers1

3

The error is in this line

NSEntityDescription *imageEntity = [NSEntityDescription entityForName:@"Thumbnail"
          inManagedObjectContext:[context valueForKey:@"image"]];

You cannot apply valueForKey:@"image" to a managed object context. You have to apply it to the fetched objects (or use the image property of the fetched object).

Note also that executeFetchRequest: returns nil only if an error occurs. If no entities are found, it returns an empty array.

NSEntityDescription *imageEntity = [NSEntityDescription entityForName:@"Thumbnail" inManagedObjectContext:context];
[fetchRequest setEntity:imageEntity];

NSError *error;
NSArray * array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (array == nil) {
    NSLog(@"Testing: Fetch error: %@", error);
} else if ([array count] == 0) {
    NSLog(@"Testing: No results found");
}else {
   Thumbnail *testEntity = [array objectAtIndex:0];
   NSImage *image = testEntity.image;
   _coreDataImageView.image = image;
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I end up with this error: Warning - attempting to decode NSCGSImageRep 2012-12-28 16:00:01.868 The Template[10703:503] *** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array When I do a NSLog of [array count] i get the answer as 5 or some number. – DesperateLearner Dec 28 '12 at 08:00
  • @DesperateLearner: I have broken the code in my answer into multiple lines. In which line exactly do you get the exception? – Martin R Dec 28 '12 at 08:08
  • @DesperateLearner: I have tested the code and could successfully save and load an `NSImage`, so it should be correct. – Martin R Dec 28 '12 at 09:49