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 12-17-2010, 10:25 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 1
ingaham is on a distinguished road
Default TableView is not update from a NSFetchedResultsController

Hy,

I'm new in Core Data, I work in a tableView, witch visualizes the corresponding element one of my datatables.
A have some tables in my database, but now just two is important, the SONG and the COLLECTION tables, there is in relationship with each other. One of my class (RootViewColtroler) I want to alloc a SongTableViewControler (code is below) to visualize the SONGS in a specified COLLECTION:
When the app is running, I select a collection calling this function:
Code:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	self.songListView = nil;
	COLLECTIONS *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
	NSUInteger i = [selectedObject.collectionContainSongs count] ;
	NSString * s = [NSString stringWithFormat: @"%d", i];
	SongTableViewController* songView = [[SongTableViewController alloc] initWithNibName:@"SongTableViewController" songs:selectedObject.collectionContainSongs managedcont:managedObjectContext title:selectedObject.name bundle:nil];
	self.songListView = songView;
	[songView release];
	[self.navigationController pushViewController:self.songListView animated:TRUE];
}
The header of my SongTableViewController class is the follow:
Code:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@class RootViewController;
@interface SongTableViewController : UITableViewController <NSFetchedResultsControllerDelegate>{
    NSFetchedResultsController *songfetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
	UINavigationBar* navBar;
//@private:
	NSPredicate *songPredicate;
	//NSMutableArray* mysongs;
}
@property (nonatomic, retain) NSFetchedResultsController *songfetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) IBOutlet UINavigationBar* navBar;
@property (nonatomic, retain) NSMutableArray* mysongs;
@property (nonatomic, retain) NSPredicate *songPredicate;

- (id)initWithNibName:(NSString *)n songs:(NSSet*)s managedcont:(NSManagedObjectContext*) _managedObjectContext title:(NSString *)_title bundle:(NSBundle *)b;
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath ;
@end
The implementation file of my SongTableViewController class is the follow
Code:
#import "SongTableViewController.h"
#import "SONGS.h"

@implementation SongTableViewController
@synthesize navBar, mysongs, managedObjectContext, songfetchedResultsController, songPredicate;

- (id)initWithNibName:(NSString *)n songs:(NSSet*)s managedcont:(NSManagedObjectContext*) _managedObjectContext title:(NSString *)_title  bundle:(NSBundle *)b
{
	if(self = [super initWithNibName:n bundle:b])
	{
		self.managedObjectContext = _managedObjectContext;
		self.title = _title;
	}
	return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	NSError *error = nil;
//I want to create a predicate for chosen the corresponding songs in the COLLECTION. It works fine.
	if (self.title != nil){ 
		self.songPredicate = [NSPredicate predicateWithFormat: @"(songBelongingToCollection.name LIKE[c] %@)", self.title];
	}
	else{
		self.songPredicate =nil;
	}
	if (![[self songfetchedResultsController] performFetch:&error]) {
		NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
		abort();
	}
}

- (void)viewWillDisappear:(BOOL)animated {
	self.songfetchedResultsController = nil;
    [super viewWillDisappear:animated];
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    SONGS *managedObject = [self.songfetchedResultsController objectAtIndexPath:indexPath];
	NSLog(@"The title of the song: %@", managedObject.name);
    [cell.textLabel setText : managedObject.name] ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
	NSLog([NSString stringWithFormat: @"section :%d", [[self.songfetchedResultsController sections] count]]);
    return [[self.songfetchedResultsController sections] count];	
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.songfetchedResultsController sections] objectAtIndex:section];
    NSLog([NSString stringWithFormat: @"rownum: %d", [sectionInfo numberOfObjects]]);
	return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	[self configureCell:cell atIndexPath:indexPath];
    return cell;
}

//The most important function, I think, this is the source of the problem. 
- (NSFetchedResultsController *)songfetchedResultsController {
    if (songfetchedResultsController != nil) {
        return songfetchedResultsController;
    }
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SONGS" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];
	if (self.songPredicate != nil) {//this part is always called, when the view is loaded, and it is valid.
		NSLog(@"predicate: %@",self.songPredicate);
		[fetchRequest setPredicate:self.songPredicate];
		self.songPredicate = nil;
	}
    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.songfetchedResultsController = aFetchedResultsController;
    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];
    return songfetchedResultsController;
}    

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
            
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.tableView;
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeUpdate:
			[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
			break;
        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSManagedObject *objectToDelete = [self.songfetchedResultsController objectAtIndexPath:indexPath];        
        NSManagedObjectContext *context = [self.songfetchedResultsController managedObjectContext];
        [context deleteObject:objectToDelete];
        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }   
}
- (void)dealloc {
    [super dealloc];
	self.songPredicate = nil;
	self.songfetchedResultsController = nil;
}
@end
When I choose a COLLECTION at first time, the valis SONGS in the screen (in the rows of the table view). But choosing an other COLLECTION, the same SONGS at the screen... I think the fetchresultcontroller is always updated (leastwise the Predicate object is updated), but the view is not updated.

Help me, what is wrong of my code?
(the 'didReceiveMemoryWarning', function is deleted by the post caused the high number of characters...)
Thanks!
ingaham is offline   Reply With Quote
Old 10-17-2011, 10:32 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 1
msec is on a distinguished road
Default

I was just wondering if you resolved your problem and if so what did you do? I'm experiencing the same problem now.

Thanks
msec is offline   Reply With Quote
Reply

Bookmarks

Tags
coredata, fetchedresultcontroller, tableview

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: 392
9 members and 383 guests
chemistry, daudrizek, HemiMG, jeroenkeij, Kirkout, PavelMik, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

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