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 のデータベースにデータを格納する方法を確認できます。

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

ベスト: 一致させようとしている Album 管理オブジェクトにアクセスできると仮定して、これを試してください:

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

プロパティの 1 つではなく、オブジェクトに一致する

于 2012-10-15T23:22:49.373 に答える