views:

17

answers:

2

Hi all,

I have a class,

@interface OnlineDataModel : TTModel < NSFetchedResultsControllerDelegate > { ...

Where TTModel is,

@interface TTModel : NSObject < TTModel > { ...

OnlineDataModel class is Singleton, so its object never expires. I am performing the Fetch operation within this class and setting the delegate of the fetchedResultsController by self. Now i am changing the objects fetched after constanct delay to just check that it gives the call backs, objects are changed and changes are reflected to the database as well, But OnlineDataModel class is not receiving the Call Backs so that my UI can get updated.

i am badly stuck in it , so any help will highly be appreciated.

Thanks in advance :)

A: 

How/Where do you set the delegate? If you set the delegate to self in a class method (has a + infront of its name instead of a - ... like + (void) doSomething) the delegate is set to the class object and not the instance object.

for me this works:

@implementation MyViewController

...

- (NSFetchedResultsController *) fetchedResultsController
{
...
    [fetchedResultsController setDelegate: self];
...
    return fetchedResultsController;
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    NSLog(@"data changed");
}

...

@end

Please post your controller-creation code :)

Jaroslaw Szpilewski
A: 

Before i post the code, i have a doubt on some thing, see the following code first

Book *book = [[[[CDBManager sharedManager] fetchedResultsController] fetchedObjects] objectAtIndex:index];
[book setTitle:@"New Title"];
[[[DatabaseContext sharedObject] managedObjectContext] save:nil];

Is this code causes any event to be fired from FetchedResultController, i guess fetchedObjects return copy of the Array. if its a copy, than definetly no call back will ever be received. But its the array of live objects than there is some thing in my code.. What you say on this ?

Ansari
If it's a copy it's only a shallow copy. From my experience the events are fired when you change a property of a object belonging to the controller's managed object context. You don't even have to call save to let the events fire - they will be fired as soon as the property change is completed. Note that if you have custom setters in your class you'll have to take care of the KVO stuff yourself for that setters. [self willAccessValueForKey:@"title"]; ... [self didAccessValueForKey:@"title"];
Jaroslaw Szpilewski
arg ... sorry. ..AccessValue should be ChangeValue: [self willChangeValueForKey:@"title"]; ... [self didChangeValueForKey:@"title"];
Jaroslaw Szpilewski