从Core Data检索单个对象

时间:2013-08-22 07:42:19

标签: ios core-data

在我的应用中,有多个画廊。所以我创建了一个名为Gallery的实体并插入了一些对象。像这样,

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);
}

现在在视图控制器中,我需要根据所选的图库检索有关特定图库的描述。

以下是我到目前为止的代码

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;
} 

如何仅检索对象,例如Art,然后显示其描述?

4 个答案:

答案 0 :(得分:2)

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
         }
    }

答案 1 :(得分:2)

具体来说,我们也可以使用谓词方法并通过传递图库名称来调用此函数(例如@“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];
 }

答案 2 :(得分:1)

您需要快速枚举所提取的请求:

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
}

答案 3 :(得分:0)

如果self.gallery从前一个传递到视图控制器(可能来自UITableView上的选择),那么为什么不能这样做:

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