我想要獲取所有相冊的圖片。我正在通過web服務獲取此信息。 Web服務具有以下佈局。NSFetchRequest不會回報任何東西
{
"name": "Club Brugge - KRC Genk",
"date": "08.10.2012",
"albumId: 1,
"pictures": [
{
"pic_album_id"=1,
"pic_id" = 1,
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg"
},
{
"pic_album_id"=1,
"pic_id" = 2,
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/e8e10c0664eb0533a0534ed69891b165.jpg"
},
{
"pic_album_id"=1,
"pic_id"= 3,
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/750b55a87b8eae33b8f3278add9bec44.jpg"
}
]
我有以下功能獲取某個相冊的所有圖片。
- (NSMutableArray *)getAllPicturesOfAlbumId: (int)AlbumId
{
NSString *picture_Url = [[NSString alloc]init];
NSArray *results = [[NSArray alloc]init];
_picturesForAlbum = [[NSMutableArray alloc]init];
picture_Url = @"";
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pic_album_id == %@",
[NSNumber numberWithInt:AlbumId]];
NSLog(@"album id: %@",[NSNumber numberWithInt:AlbumId]);
[request setEntity:[NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.genkDatabase.managedObjectContext]];
[request setPredicate:predicate];
NSError *error = nil;
results = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"results: %@",results);
if (results == nil) {
NSLog(@"nil results");
// handle errors
} else if (results.count == 0) {
NSLog(@"0 results");
// nothing found
} else {
for(int i = 0; i < results.count ; i++){
Picture *picture = [results objectAtIndex:i];
NSLog(@"%@",[results objectAtIndex:i]);
[_picturesForAlbum addObject:picture];
}
}
NSLog(@"%@",_picturesForAlbum);
//NSLog(@"album: %@",[_picturesForAlbum objectAtIndex:5]);
return _picturesForAlbum;
}
上面的代碼給出了以下日誌。 (我給了testcase AlbumId = 3)
2012-10-12 14:53:04.577 RacingGenk[4793:c07] album id: 3
2012-10-12 14:53:04.578 RacingGenk[4793:c07] results: (
)
2012-10-12 14:53:04.578 RacingGenk[4793:c07] (
)
希望有人能幫助我。
親切的問候。
**這裏你可以看到我的數據庫模型
編輯
這裏你可以看到我是如何得到我的照片,並把它放在我的數據庫的screenshot。
+ (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 albumWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext withAlbumId:albumId];
for (NSDictionary *genkInfo2 in pictures) {
pictureId++;
[Picture pictureWithGenkInfo:genkInfo2 inManagedObjectContext:document.managedObjectContext withAlbumId:albumId withPictureId:pictureId];
}
pictureId = 0;
// table will automatically update due to NSFetchedResultsController's observing of the NSMOC
}
這很奇怪,你從圖片實體獲取結果,但是使用謂詞相冊ID。我認爲在謂詞應該是:[NSPredicate predicateWithFormat:@「album.pic_album_id ==%@」,numberWithInt:AlbumId]]; – NeverBe
對不起,我提供了錯誤的json數據。我編輯了我的代碼。 – Steaphann
你有沒有Album實體?這個實體應該有圖片一對多的關係。然後你可以得到所有picuture只是調用 - > NSArray *圖片= [album.pictures allObjects]; – NeverBe