Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-28-2010, 04:50 PM   #1 (permalink)
Martyn
Registered Member
 
Join Date: Dec 2009
Posts: 35
Martyn is on a distinguished road
Exclamation NSPredicate Query

Good evening Sirs,

I am having a little trouble performing a 'query' using NSPredicate on two entities in my Core Data project.

The two entities are:

Author {authorName, dateOfBirth}
Book {authorName, bookName, pages}

I have setup the relationships correctly (one author has many books, inverse).

At the moment I can click on an author in a view controller, and it will take me to a detail view controller that will display all books by all authors. I would much rather the detail view controller displayed all the books by a certain author (the one that is clicked in the view controller).

I presume I need to do a NSPredicate Query to achieve this? So far I have:

Code:
	// Gets new name of author.
	NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
	NSString *newAuthorName = [[selectedObject valueForKey:@"authorName"] description];
	
	// Begin predicate query.
	NSFetchRequest *myFetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Books" inManagedObjectContext:managedObjectContext_];
    [myFetchRequest setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@==%@", newAuthorName, authorName];
    [myFetchRequest setPredicate:predicate];
    NSError *error;
	NSArray *listArray;
    listArray = [[managedObjectContext_ executeFetchRequest:myFetchRequest error:&error] mutableCopy];
	NSLog(@"listArray: %@", listArray);
I would expect that this would return all the books by a certain author (the relevant author name is passed into the detail view controller from the prior view controller).

However, I am not sure I am using the correct code for the predicate as they are my weakness.

Also, I am a little unsure what to do with the returned 'listArray'.

I would be very grateful if anyone could help me out here. I think I have explained the problem pretty well, but if more clarity is needed just ask
Martyn is offline   Reply With Quote
 

» Advertisements
» Online Users: 366
14 members and 352 guests
AlanR917, AyClass, blakeart113, Bryan1, dineshprasanna, ipodphone, Kirkout, martianflash, mutank, Newbie123, nguyenhieu129, NSString, Objective Zero, olivia325
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,566
Threads: 94,081
Posts: 402,751
Top Poster: BrianSlick (7,990)
Welcome to our newest member, blakeart113
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:05 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.