0

我正在尝试使用 NSPredicates。但它总是给我同样的数组。在这里你可以看到我的谓词。

NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whichAlbum.album_id == %d", AlbumId];
    [request setEntity:[NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.genkDatabase.managedObjectContext]];
    [request setPredicate:predicate];

另外,当我尝试硬编码时。它返回相同的数组。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whichAlbum.album_id == 5"];

我的数据库模型是:

在此处输入图像描述

在这里您可以看到我如何将数据放入我的数据库中以获取实体图片。

+ (Picture *)pictureWithGenkInfo:(NSDictionary *)genkInfo
          inManagedObjectContext:(NSManagedObjectContext *)context
                     withAlbumId:(int)albumId
                   withPictureId:(int)pictureId;
{

    Picture *picture = nil;

    picture = [NSEntityDescription insertNewObjectForEntityForName:@"Picture"
                                          inManagedObjectContext:context];
    picture.url                     = [genkInfo objectForKey:PICTURES_URL];
    picture.pic_album_id            = [NSNumber numberWithInt:albumId];
    picture.picture_id              = [NSNumber numberWithInt:pictureId];


    return picture;
}

任何人都可以帮助我吗?

亲切的问候 编辑

  for (NSDictionary *genkInfo in albums ) {
        albumId++;
        Album *album = [Album albumWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext withAlbumId:albumId];
        for (NSDictionary *genkInfo2 in pictures ) {
            pictureId++;
            Picture *pic = [Picture pictureWithGenkInfo:genkInfo2 inManagedObjectContext:document.managedObjectContext withAlbumId:albumId withPictureId:pictureId];
            [album addPicturesObject:pic]; // this method should be automatically generated
        }
        pictureId = 0;
        // table will automatically update due to NSFetchedResultsController's observing of the NSMOC
    }
4

2 回答 2

0

只需删除whichAlbum并尝试:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"album_id == %d", AlbumId];

于 2012-10-15T22:57:43.180 回答
0

更好的是,假设 AlbumId 的值是某种数字原语:

old style:
[NSPredicate predicateWithFormat:@"whichAlbum == %@", [NSNumber numberWithInt:AlbumId]];
modern style: (xcode 4.5)
[NSPredicate predicateWithFormat:@"whichAlbum == %@", @(albumId)];

看起来可能 只会在其格式字符串中生成predicateWithFormat:正确的谓词。%@@K

最佳:假设您有权访问您尝试匹配的相册托管对象,请尝试以下操作:

[NSPredicate predicateWithFormat:@"whichAlbum == %@", album];

匹配对象,而不是其属性之一

于 2012-10-15T23:22:49.373 回答