Reputation: 2777
I am trying to get the url of the first picture of an album. I am getting my data from a json webservice. This webservice looks like this.
"albums": [
{
"name": "Club Brugge - KRC Genk",
"date": "08.10.2012",
"pic_album_id" : 1
"pictures": [
{
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg",
"picture_id" : 1
},
{
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/e8e10c0664eb0533a0534ed69891b165.jpg"
"picture_id" : 2
},
{
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/750b55a87b8eae33b8f3278add9bec44.jpg"
"picture_id" : 3
},
{
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/5867e1dedd00d08d830fd7f098a0b747.jpg"
"picture_id" : 4
}
I am trying to get only the first URL. I am trying to get this with the following function.
- (NSString *)getFirstPictureOfAlbumId: (int)AlbumId
{
NSString *picture_Url = [[NSString alloc]init];
NSLog(@"here1");
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSLog(@"here2");
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(picture_id == 1) AND (pic_album_id == %@)",AlbumId];
NSLog(@"here3");
[request setEntity:[NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.genkDatabase.managedObjectContext]];
NSLog(@"here4");
[request setPredicate:predicate];
NSLog(@"here5");
NSError *error = nil;
NSArray *results = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
if (error != nil)
{
//handle errors
}
NSLog(@"picture url is: %@",[results valueForKey:@"url"]);
picture_Url = [results valueForKey@"url"];
return picture_Url;
}
My logs only reaches to "here2" So I think there is something wrong with my predicate. Another problem I have is to store the url of the first picture.
Can anybody help me?
Thanks in advance
EDIT
This is what I do in my tableview
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[self getFirstPictureOfAlbumId:indexPath.row+1]]];
NSLog(@"indexpatch is: %d",indexPath.row+1);
NSLog(@"first picture is: %@",[self getFirstPictureOfAlbumId:indexPath.row+1]);
And then it logs it like this.
012-10-09 13:17:58.140 RacingGenk[6069:c07] album id is: 1
2012-10-09 13:17:58.311 RacingGenk[6069:c07] indexpatch is: 1
2012-10-09 13:17:58.311 RacingGenk[6069:c07] album id is: 1
2012-10-09 13:17:58.313 RacingGenk[6069:c07] first picture is: http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
2012-10-09 13:17:58.314 RacingGenk[6069:c07] album id is: 2
2012-10-09 13:17:58.445 RacingGenk[6069:c07] indexpatch is: 2
2012-10-09 13:17:58.445 RacingGenk[6069:c07] album id is: 2
2012-10-09 13:17:58.446 RacingGenk[6069:c07] first picture is: http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
2012-10-09 13:17:58.447 RacingGenk[6069:c07] album id is: 3
2012-10-09 13:17:58.577 RacingGenk[6069:c07] indexpatch is: 3
2012-10-09 13:17:58.577 RacingGenk[6069:c07] album id is: 3
2012-10-09 13:17:58.578 RacingGenk[6069:c07] first picture is: http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
EDIT2
LOG
RacingGenk[6605:c07] picture_id=1, album_id=1, url=http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
2012-10-09 13:57:40.361 RacingGenk[6605:c07] picture_id=1, album_id=2, url=http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
2012-10-09 13:57:40.525 RacingGenk[6605:c07] picture_id=1, album_id=3, url=http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
Upvotes: 0
Views: 154
Reputation: 539685
In your predicate
[NSPredicate predicateWithFormat:@"(picture_id == 1) AND (pic_album_id == %@)", AlbumId];
you use "%@" as format specifier (which is for objects), but an int argument AlbumId
.
If the "pic_album_id" attribute is stored as NSNumber
in your Core Data model, then the predicate must be
[NSPredicate predicateWithFormat:@"(picture_id == 1) AND (pic_album_id == %@)",
[NSNumber numberWithInt:AlbumId]];
(Note: In Objective C, it is very common to let variable names start with a lower case letter, i.e. albumId
instead of AlbumId
. Using that convention makes your code easier to read for others in this forum.)
EDIT: To get the first picture that is in an album with given albumId
, the following predicate should work:
[NSPredicate predicateWithFormat:@"(picture_id == 1) AND (album.pic_album_id == %@)",
[NSNumber numberWithInt:albumId]];
This code assumes that you have an inverse relationship album from the Picture entitiy to the Album entity (inverse relationship to pictures).
Even if there is only one result, it is stored in an array:
NSArray *results = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
if (results == nil) {
// handle errors
} else if (results.count == 0) {
// nothing found
} else {
Picture *picture = [results objectAtIndex:0];
NSLog(@"picture_id=%@, album_id=%@, url=%@", picture.picture_id, picture.album.pic_album_id, picture.url);
pictureUrl = [picture url];
}
Upvotes: 1
Reputation: 2814
I am using NSPredicate before and maybe you could use this
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == 1"];
Use the EXACT variable names for NSPredicate as what is returned from your webservice.
Upvotes: 0