Isuru
Isuru

Reputation: 31283

Retrieving a single object from Core Data

In my app, there are multiple galleries. SO I have created an entity called Gallery and inserted a few objects. Like so,

NSManagedObjectContext *context = [self managedObjectContext];

Gallery *gallery1 = [NSEntityDescription insertNewObjectForEntityForName:@"Gallery" inManagedObjectContext:context];
gallery1.galleryId = @1;
gallery1.galleryName = @"Art";
gallery1.galleryDesc = @"Some info about art";

Gallery *gallery2 = [NSEntityDescription insertNewObjectForEntityForName:@"Gallery" inManagedObjectContext:context];
gallery2.galleryId = @2;
gallery2.galleryName = @"Architecture";
gallery2.galleryDesc = @"details about architecture";

NSError *error;
if (![context save:&error]) {
    NSLog(@"Error Ocurred When Saving: %@", error.localizedDescription);
}

Now in a view controller, I need to retrieve the descriptions about a particular gallery depending on the selected gallery.

Below is the code I have so far

NSFetchRequest *fetchRequst = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Gallery" inManagedObjectContext:self.managedObjectContext];
fetchRequst.entity = entity;
NSError *error;
// Gets the Gallery objects to an array
self.galleries = [self.managedObjectContext executeFetchRequest:fetchRequst error:&error];

// self.gallery is passed in to this view controller from the previous one
if ([self.gallery isEqualToString:@"Art"]) {
    self.galleryInfoTextView.text = gallery.galleryDesc;
} else if ([self.gallery isEqualToString:@"Architecture"]) {
    self.galleryInfoTextView.text = gallery.galleryDesc;
} 

How can I retrieve only the object for example, for Art, and then display its description?

Upvotes: 1

Views: 88

Answers (4)

anky_believeMe
anky_believeMe

Reputation: 484

To be specific we can also use the predicate method and call this function by passing the gallery name(e.g @"Art", @"Architecture")

+ (Gallery *)getGalleryFor:(NSString *)galleryName
 {
      NSFetchRequest *fetchRequst = [[NSFetchRequest alloc] init];
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"Gallery" inManagedObjectContext:self.managedObjectContext];
      [fetchRequest setEntity:entity];
      NSPredicate *pred = [NSPredicate predicateWithFormat:@"galleryName == %@",galleryName];
      [request setPredicate:pred];
      NSError *error;
      NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

     return [array lastObject];
 }

Upvotes: 2

Anurag Soni
Anurag Soni

Reputation: 1077

NSArray *galleryArray = [self.managedObjectContext executeFetchRequest:fetchRequst error:&error];


    for (Gallery *gallery in galleryArray)
    {
        if(gallery.galleryName isEqualToString:@"Art"){
            //this is gallery Object is of Art
         } else if (gallery.galleryName isEqualToString:@"Architecture"){
             //this is gallery Object is of Architecture
         }
    }

Upvotes: 2

yeesterbunny
yeesterbunny

Reputation: 1847

You need to fast enumerate through the fetched requests:

NSFetchRequest *fetchRequst = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Gallery" inManagedObjectContext:self.managedObjectContext];
fetchRequst.entity = entity;
NSError *error;
// Gets the Gallery objects to an array
self.galleries = [self.managedObjectContext executeFetchRequest:fetchRequst error:&error];

// self.gallery is passed in to this view controller from the previous one

for(Gallery *g in self.galleries){
    if([g.galleryName isEqualToString:@"Art"]){
        self.galleryInfoTextView.text = g.galleryDesc;
    }
    //blah blah blah
}

Upvotes: 1

Steve Wilford
Steve Wilford

Reputation: 9002

If self.gallery is passed into the view controller from the previous one (presumably from a selection on a UITableView) then why can't you just do this:

self.galleryInfoTextView.text = self.gallery.galleryDesc;

Upvotes: 0

Related Questions