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 06-07-2010, 12:41 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 48
cybohemia is on a distinguished road
Default How to add 2nd entity to Core Data Navigation app

I created an iPhone app using XCode's Navigation-based Application template, which creates the Core Data infrastructure in the generated code.

I replaced the generated Event entity with my own Company entity. All the database infomation shows up in the viewTable. So far so good.

Now I create a second entity called Group and I use the one viewTable to show both data sets, depending on a toggle switch setting:
  1. The viewTable is able to switch between the two sets of data ok if the data is retrieved from a database directly.
  2. It is also able to display the data from Core Data properly for the table that is shown upon initialization, whichever table I set that to.
But, with Core Data, when I use the switch to toggle tables, the data isn't updated. I don't think this isn't about reloadData since I had that working ok with data from the database. Actually, the numberOfSections is 0 so the data just isn't being retrieved.

I tried printing out the internal calls to the database to figure out what's going on. It seems that the database calls are being invoked just after the fetchedResultsController method and just before the numberOfSectionsInTableView. (Does Core Data run in a background thread but is then synchronized with the main thread?) After I switch from Company to Group, fetchedResultsController is called again but, this time, there are no database calls made before numberOfSectionsInTableView is called.

Although Core Data seems to save some coding compared to sqlite, I feel lost because I can't see where the things are happening. How do I can my hands on the invocation to the database so I can force a fetch?

Anyone have any ideas?

Thanks!
cybohemia is offline   Reply With Quote
Old 06-07-2010, 02:12 AM   #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

I'm a little confused, especially about your comment regarding returning 0 for numberOfSectionsInTableView, in which case of course you won't see any data.

Can you explain further, and perhaps post some of your UITableViewDataSource methods? You can definitely accomplish what you are trying to do, although it does get a little more complicated when you are juggling multiple NSFetchedResultsControllers in a single table view controller.
__________________
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 06-07-2010, 08:21 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 48
cybohemia is on a distinguished road
Default

The 0 for numberOfSectionsInTableView is definitely confusing. I actually haven't changed the code from the Data Source section at all (from the auto-generated code). For example, numberOfSectionsInTableView remains unchanged:

Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[fetchedResultsController sections] count];
}
The only major changes I've made to try to get things to work was in fetchedResultsController, and even that didn't stray too far from the original - I just did a switch on the entity creation and took out the cacheName (in case it was caching the old stuff):

Code:
- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }
    
    /*
     Set up the fetched results controller.
    */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.

    NSEntityDescription *entity;
	switch (mode) {
		case COMPANY:			
			entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
			break;
		case GROUP:			
			entity = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:managedObjectContext];
			break;
	}			
    [fetchRequest setEntity:entity];
    
    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];
    
    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] 
					 initWithFetchRequest:fetchRequest 
					 managedObjectContext:managedObjectContext 
					 sectionNameKeyPath:nil 
					 cacheName:nil]; //@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
    
    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];
    
    return fetchedResultsController;
}
What I'm trying to do is to get the the fetchedResultsController method to re-fetch the data but with the new entity I've switched to (GROUP rather than COMPANY). I've set fetchedResultsController to nil when I toggle from COMPANY to GROUP just to make sure the fetchedResultsController gets past the initial conditional. I've had NSLog() calls to show that it did get through to the end of the method but it seems that it was unable to retrieve anything and so is returning 0 for the section num.

I wonder if there some sort of re-initialization I need to do to make sure the fetchedResultsController is set up properly...?
cybohemia is offline   Reply With Quote
Old 06-07-2010, 11:01 AM   #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

Quote:
Originally Posted by cybohemia View Post
I wonder if there some sort of re-initialization I need to do to make sure the fetchedResultsController is set up properly...?
I would try doing this before the refetch:

Code:
[NSFetchedResultsController deleteCacheWithName:nil];
However, I think that setting the fetchedResultsController to nil and then reinvoking the getter is definitely working against the design of the typical Core Data boilerplate code.

I would think it would be much, much better to have two table views and two fetched results controllers... and each with its own named cache, which you will still need to call the delete method on, BTW, if/when you change your query.
__________________
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 06-07-2010, 04:42 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 48
cybohemia is on a distinguished road
Default

I had originally wanted to have two views but somewhere along the line, that didn't work out.

Having two fetched results controllers...that did the trick!

I guess it wasn't immediately obvious to me that I couldn't just re-use the one fetched results controller and had to create a new object to fetch the data.

Thanks!
cybohemia is offline   Reply With Quote
Reply

Bookmarks

Tags
core data, navigation, thread

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: 335
12 members and 323 guests
Absentia, Domele, fiftysixty, givensur, heshiming, iGamesDev, linkmx, michaelhansen, PixelInteractive, raihan.zbr, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,117
Posts: 402,891
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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