0

在此处输入图像描述

我在检索通过 Menu 实体创建的项目时确实遇到了麻烦。这是我用来将项目添加到特定菜单对象的代码

-(void)additem:(NSString *)entity :(NSDictionary *)aDictionary :(NSString *)menu
{
   NSLog(@"additem");
   NSError *error = nil;

   Menu *menuItem = nil;

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

[request setPredicate:[NSPredicate predicateWithFormat:@"mname=%@ and msection =%@",@"Parents",@"Keydates"]];

menuItem = [[managedObjectContext executeFetchRequest:request error:&error] lastObject];

if (error) {
    //Handle any errors
}
if (!menuItem) {
    //Nothing there to update
    NSLog(@"This class doesn't exist");
}

Items *anitem = (Items *)[NSEntityDescription insertNewObjectForEntityForName:@"Items" inManagedObjectContext:managedObjectContext];;
anitem.type = [aDictionary objectForKey:@"type"];
anitem.title = [aDictionary objectForKey:@"title"];
anitem.image = [aDictionary objectForKey:@"image"];
anitem.subtitle = [aDictionary objectForKey:@"subtitle"];

[menuItem addItemsObject:anitem];

[managedObjectContext save:&error];

}

我想使用谓词来检索在特定 Menu 对象上创建的所有项目。这是我试图检索它的代码。

- (void) readItems: (NSString *)section {


    NSLog(@"readItems");
    NSError *error = nil;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Menu"
                                          inManagedObjectContext:managedObjectContext];
    fetchRequest.resultType = NSDictionaryResultType;

    [fetchRequest setEntity:entity];

    [fetchRequest setReturnsObjectsAsFaults:NO];

    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"mname=%@ and msection = %@",@"Parents",@"Keydates"]];


    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

    for (Items *item in fetchedObjects) {
        NSLog(@">>>>>>%@",item);
    }
}

谁能指出我正确的方向。我知道我通过了部分并且不使用它。我已将实际值放入。

4

1 回答 1

0

Your readItems: method specifies the entity for your fetch as Menu yet when you execute the fetch you are expecting Item (since you start iterating through fetchedObjects and casting them as Item).

Instead what you want to do is et your entity to Item and change the predicate to

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"items.mname=%@ and items.msection = %@",@"Parents",@"Keydates"]];

Furthermore you should consider renaming your relationships. The relationship field specifies exactly what you would expect to see when you follow the arrow. Thus when I look at Items and follow its arrow to Menu I would expect the name of this relationship to be menu yet you call it menuItems. You have done this correctly for the relationship from Menu to Item.

That way we would end up with a more readable predicate looking like:

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"menu.mname=%@ and menu.msection = %@",@"Parents",@"Keydates"]];

Happy Coding

于 2014-08-07T17:01:00.943 回答