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 09-14-2010, 09:25 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Setauket, NY
Posts: 133
hstaniloff is on a distinguished road
Question Dynamically Updating FetchedResultsController?

I'm trying to change the sort of fetchedresultscontroller. Is this possible to do or do I have create another FRC with a different sortDescriptor?
Here's my code, but it doesn't work - only shows a blank table when inserted in the viewWillLoad callback. Any ideas how to do this? Thanks!
Code:
[self getAppSettingSortOrder];//Sets currentSortOrder for the class.     

if (sortOrderChanged) {

    //Form a new fetchrequest for the FRC and reload the table.

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Aircraft" inManagedObjectContext:managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:currentSortOrder ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSError *error = nil;

    [fetchedResultsController performFetch:&error];

    [self.tableView reloadData];

    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];      
}
hstaniloff is offline   Reply With Quote
Old 09-15-2010, 08:40 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 78
bigdiggyiPhone is on a distinguished road
Default

Hi,

Take a quick look at the nsfetchedresultscontroller docs and you will see that the fetchRequest property can't be changed dynamically.

You will have to recreate the entire nsfetchedresultscontroller. What I would do is create it lazily. That way when your change your sort order, you set self.fetchedresultsController =nil

Then when the fetched results controller is called by y our tableView, it gets recreated with the new sort order.

do a search on my other posts and you will find one that talks about lazily creating hour nsfetchedresultscontrollerresults controller and nsfetchedresultscontroller nsfetchrequest.

Per apple's docs, don't forget to delete the cache if you change the nsfetchedresultscontroller!

Good luck

T


Quote:
Originally Posted by hstaniloff View Post
I'm trying to change the sort of fetchedresultscontroller. Is this possible to do or do I have create another FRC with a different sortDescriptor?
Here's my code, but it doesn't work - only shows a blank table when inserted in the viewWillLoad callback. Any ideas how to do this? Thanks!
Code:
[self getAppSettingSortOrder];//Sets currentSortOrder for the class.     

if (sortOrderChanged) {

    //Form a new fetchrequest for the FRC and reload the table.

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Aircraft" inManagedObjectContext:managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:currentSortOrder ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSError *error = nil;

    [fetchedResultsController performFetch:&error];

    [self.tableView reloadData];

    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];      
}
__________________
Check out my apps:
  • Friis-It NF. Easily calculate the Noise Figure, Sensitivity, and gain of an RF receiver design.
  • RetireMax. Get a dose of reality about that next purchase!
bigdiggyiPhone is offline   Reply With Quote
Old 09-16-2010, 06:08 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Setauket, NY
Posts: 133
hstaniloff is on a distinguished road
Question

Thanks for the information!
I did a search on your other posts, but can't identify any that talk about creating the FRC lazily.
So, what you are saying is that when my sort order changes, I can assign my class variable FRC to nil, re-create the sortDescriptor and then reload the tableView and the FRC will get recreated with the new sortDescriptor?
Do you have any code you can point me to? I think I get it though if that's the case.
hstaniloff is offline   Reply With Quote
Old 09-16-2010, 07:37 AM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Setauket, NY
Posts: 133
hstaniloff is on a distinguished road
Default

I figured it out. Here's what I did. Works like a champ!
Code:
- (void)viewWillAppear:(BOOL)animated {
	
	[self appSettingSortOrder];
	
	if (sortOrderChanged) {
		//Delete the cache file that's created from for the frc.
		[NSFetchedResultsController deleteCacheWithName:@"Aircraft"];

		self.fetchedResultsController = nil;

		//Fetch existing aircraft out of the data store.
		NSError *error = nil;
		if (![[self fetchedResultsController] performFetch:&error]) {
			NSString *errorMessage = [[NSString alloc] initWithFormat:@"Domain: %@ Code: %D",[error domain], [error code]];
 
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unrecoverable Database Error" 
			message:errorMessage  delegate:nil
			cancelButtonTitle:@"Press the Home Button to Exit" otherButtonTitles:nil];

			[alert show];
			[alert release];
		}
//Reload the table with the new frc/sort order.
		[self.tableView reloadData];
	}
 
}
hstaniloff is offline   Reply With Quote
Reply

Bookmarks

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: 325
19 members and 306 guests
2ndSegment, bignoggins, cayladv57, cgokey, dermotos, djohnson, Domele, guusleijsten, Hamad, heshiming, linkmx, markuschow, Objective Zero, pungs, Rudy, Sloshmonster, teebee74, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

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