使用谓词获取核心数据值 [英] fetching core data values using predicate

查看:72
本文介绍了使用谓词获取核心数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my sqlite have items in one of the entity and i wish to retrieve them out according to their categoryID

例如categoryID 1有7个项目,categoryID 2有5个项目,当我单击IBAction时,将使用此方法并根据我的global.clickedTag

e.g. categoryID 1 have 7 items and categoryID 2 have 5 items , when i clicked on an IBAction it will come to this method and retrieve according to my global.clickedTag

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

but i'm unable to retrieve the values , any idea why?

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

}

推荐答案

您遇到很多问题:

此行:

-(NSMutableArray*)fetchItem:array{

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

... is not a proper method definition. It should look like:

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

下一行:

[self managedObjectContext];

...除了调用访问器外什么都不做,但是返回被忽略.编译器会抱怨这一点,您不应忽略它.

... does nothing except call the accessor but then the return is ignored. The compiler will complain about that and you shouldn't ignore it.

您的主要问题在这里:

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

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

... because you don't use the self.managedObjectContext or [self managedObjectContext] to access the context. That makes it likely you will get a nil context or one you don't want. You make the same mistake here:

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

顺便说一句,您这里不需要可变数组,也不想复制它.您最终将获得重复的托管对象,这将导致各种问题.某处有一些参考资料使用了此表格,但此后已被更正.

BTW, you don't need a mutable array here and you don't want to copy it. You will end up with duplicate managed objects which will cause all kinds of problems. There was some reference material somewhere that used this form but it has since been corrected.

以以下内容开头的整个块:

The entire block that starts with:

if ([mutableFetchCategory count] > 0) 

毫无意义.它只是创建一个已经复制的数组的进一步复制.

is pointless. It just creates further duplication of an already copied array.

您正在使它变得比所需复杂得多.只需在Xcode4中使用带谓词的核心数据提取"代码段并填写空白即可.

You are making this a lot more complicated than it needs to be. Just use the "Core Data Fetch With Predicate` snippet in Xcode4 and fill in the blanks.

-(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子类.

fetchedObjects will be an array of either NSManagedObject objects or IItem objects depending on on whether you provided a custom NSManagedObject subclass or not.

这篇关于使用谓词获取核心数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆