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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-11-2011, 01:12 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: The Midwest, USA
Posts: 6
geerlingguy is on a distinguished road
Default Help setting up a fetchRequest?

I have a navigation-based view setup, where I have a MKMapView in the first view that loads, with a bunch of annotations.

When the annotation's detail disclosure is tapped, I push into a second view display (a detail view), and the annotation passes a title and a parishNumber to the detail view.

What I'd like to do is use that parishNumber to lookup the values for one 'Parish' record in my core data store, using a fetchRequest.

However, my familiarity with Core Data is less than poor, and I was wondering if anyone had any good pointers for doing a query like this against Core Data (something like grabbing one row in SQL with a WHERE clause like "WHERE parishNumber = 123").

I can get the entire array using a fetch request that gets all the objects like the following:

Code:
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
	for (Parish *info in fetchedObjects) {
	// do stuff here
}
...but I don't know how to just grab one 'row' from Core Data. Surely there's a more performant option than getting the entire array again?
geerlingguy is offline   Reply With Quote
Old 02-11-2011, 06:24 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Take a look at NSPredicate. You would do something like this before calling executeFetchRequest:

Code:
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"(parishNumber == %d)", parishNumber];
[fetchRequest setPredicate:searchPredicate];
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 02-12-2011, 08:32 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: The Midwest, USA
Posts: 6
geerlingguy is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
Take a look at NSPredicate. You would do something like this before calling executeFetchRequest:

Code:
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"(parishNumber == %d)", parishNumber];
[fetchRequest setPredicate:searchPredicate];
Ah, didn't know about NSPredicate — thanks!

On a related note - do you think it would be better to set up one NSArray of NSDictionaries for the entire set of data at the beginning of the class, and use that array to retrieve individual parishes (based on their parishNumbers), or just query Core Data each time?

The reason I ask is that, coming from a PHP background, I know that it's often quicker to keep the data in memory and query it there, than to query an external database more than once... does this apply with the Core Data model, too?
geerlingguy is offline   Reply With Quote
Old 02-12-2011, 11:47 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

If it were me, I think I'd just go with running the queries rather than keeping everything in memory. And then only move to keeping things all in memory if the performs seems to be lagging... but bearing in mind that you might start seeing RAM issues as a result (not sure how large your data set is, though).

Also, SQLite isn't going to have the out-of-process overhead like you'd see querying MySQL or PostgreSQL or whatever from your PHP code.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 02-21-2011, 12:34 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: The Midwest, USA
Posts: 6
geerlingguy is on a distinguished road
Default

Thanks for the info - I haven't done too much performance profiling in the app so far, but I might try both routes (I have both working) with some larger datasets (I have one table with over 3,000 rows), and threading, to see if I can optimize the lag time for processing.

Right now it looks like my smaller dataset might perform best if I load it in memory at app launch, and my larger set would be best used by individual queries throughout the app's views.
geerlingguy is offline   Reply With Quote
Reply

Bookmarks

Tags
core data, data, fetchrequest

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 374
15 members and 359 guests
blasterbr, buggen, Clouds, dre, EvilElf, jeroenkeij, jimmyon122, LEARN2MAKE, Mah6447, n00b, nyoe, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:51 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0