使用谓词获取核心数据值

发布于 2024-11-29 03:27:44 字数 2048 浏览 0 评论 0原文

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

例如,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;

}

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

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2024-12-06 03:27:44

您有很多问题:

这一行:

-(NSMutableArray*)fetchItem:array{

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

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

下一行:

[self managedObjectContext];

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

您的主要问题在这里:

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

...因为您不使用 self.managementObjectContext[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 子类。

You have a lot of problems:

This line:

-(NSMutableArray*)fetchItem:array{

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

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

The next line:

[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.

Your major problem is here:

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

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

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 will be an array of either NSManagedObject objects or IItem objects depending on on whether you provided a custom NSManagedObject subclass or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文