使用谓词获取核心数据值

时间:2011-08-11 02:15:32

标签: xcode sqlite core-data nspredicate nsfetchrequest

我的sqlite在其中一个实体中有项目,我希望根据它们的categoryID检索它们

  

e.g。 categoryID 1有7个项目,categoryID 2有5个项目,当我点击IBAction时,它会转到这个方法并根据我的global.clickedTag检索

但是我无法检索这些值,知道为什么?

-(NSMutableArray*)fetchItem:array{
[self managedObjectContext];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"IItem" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    //filter here
    NSPredicate * predicate  = [NSPredicate predicateWithFormat:@"categoryID == %i",myGlobal.clickedTag];
    [request setPredicate:predicate];
    [request setEntity:entity];


    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];
    NSError *error;

    NSMutableArray* mutableFetchCategory = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    if (!mutableFetchCategory) {
        // Handle the error.
    }


    if ([mutableFetchCategory count] > 0) 
    {

        for (NSManagedObject *info in mutableFetchCategory)
        {
            if ([[info valueForKey:@"categoryID"]intValue] == myGlobal.clickedTag)
            {
            NSLog(@"ID: %@", [info valueForKey:@"ID"]);
            NSLog(@"category NAME/photo: %@", [info valueForKey:@"name"]);
            NSLog(@"categoryID : %@",[info valueForKey:@"categoryID"]);

            Item * i = [[Item alloc]initWithTitle:[info valueForKey:@"ID"] name:[info valueForKey:@"name"] description:[info valueForKey:@"desc"] thumbnail:[info valueForKey:@"thumbnail"] photo:[info valueForKey:@"photo"] categoryID:[info valueForKey:@"categoryID"]];

            [array addObject:i];
            NSLog(@"array count : %i",[array count]);

            }
        }

    }
    [mutableFetchCategory release];
    [request release];
    return array;

}

1 个答案:

答案 0 :(得分:5)

你有很多问题:

这一行:

-(NSMutableArray*)fetchItem:array{

...不是正确的方法定义。它应该看起来像:

-(NSMutableArray*)fetchItem:(NSMutableArray *) array{

下一行:

[self managedObjectContext];

...除了调用访问器之外什么也不做,但忽略了返回。编译器会抱怨它,你不应该忽略它。

你的主要问题是:

   NSEntityDescription *entity = [NSEntityDescription entityForName:@"IItem" inManagedObjectContext:managedObjectContext];

...因为您没有使用self.managedObjectContext[self managedObjectContext]来访问上下文。这使得你可能会获得一个零上下文或一个你不想要的上下文。你在这里犯了同样的错误:

 NSMutableArray* mutableFetchCategory = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
顺便说一下,你在这里不需要一个可变数组,你不想复制它。您将最终得到重复的托管对象,这将导致各种问题。有些参考资料使用过这种形式,但后来又得到纠正。

以:

开头的整个块
if ([mutableFetchCategory count] > 0) 

毫无意义。它只是创建了已复制数组的进一步复制。

你使这变得比它需要的复杂得多。只需在Xcode4中使用“Core Data Fetch With Predicate”片段并填写空格。

-(NSArray *) fetchIItemsWithCategoryID:(NSNumber *) catID{
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"IItem"
  inManagedObjectContext:self.managedObjectContext];
  [fetchRequest setEntity:entity];

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categoryID==%@",
  catID];
  [fetchRequest setPredicate:predicate];

  NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"ID" ascending:YES];
 [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

  NSError *error = nil;
  NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
  if (fetchedObjects == nil) {
    //... error handling code
  }

  [fetchRequest release];
  return fetchedObjects; //fetchedObjects will always exist although it may be empty
}

fetchedObjects将是NSManagedObject对象或IItem对象的数组,具体取决于您是否提供了自定义NSManagedObject子类。