If the code assigns a delegate to the NSFetchedResultsController as follows:

-(id)initWithFetchedResultsController:(NSFetchedResultsController*)fetchedResultsController
{
    self = [super init];
    if (self != nil)
    {
        fetchedResultsController.delegate = self;
        _fetchedResultController = fetchedResultsController;
    }
    return self;
}

The NSFetchedResultsControllerDelegate method – controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: is not ever called on self:

-(void)controller:(NSFetchedResultsController *)controller
  didChangeObject:(id)anObject
      atIndexPath:(NSIndexPath *)indexPath
    forChangeType:(NSFetchedResultsChangeType)type
     newIndexPath:(NSIndexPath *)newIndexPath
{
    NSLog(@"Delegate method called"); // Never called
}

There is no problem with the predicate, a predicate is assigned, and working with notifications directly works.

有帮助吗?

解决方案

The problem is that an initial - (BOOL)performFetch:(NSError **)error is never called since this particular code only cares about changes to the result set, not the initial set. However, it would follow that the controller would not notice changes in data it never had. Changing the init method as follows fixes the problem:

-(id)initWithFetchedResultsController:(NSFetchedResultsController*)fetchedResultsController
{
    self = [super init];
    if (self != nil)
    {
        fetchedResultsController.delegate = self;
        _fetchedResultController = fetchedResultsController;
        [fetchedResultsController performFetch:nil];
    }
    return self;
}

其他提示

I had a NSFetchedResultsController with a delegate set and performFetch executed. And still delegate methods did not get called.

My Fetched Results Controller had a sectionNameKeyPath which was a Boolean value. I was accessing in as a non-primitive - as an NSNumber.

All entities I was inserting and updating had this property nil. I was expecting that nil will be interpreted as NO, but this wasn't the case.

When I inserted an entity with this property being nil, fetched results controller delegate method would not get called at all. It would simply ignore this record like it doesn't concern my FRC.

So, I made sure I am never putting nils in the property that is used as sectionNameKeyPath.

I have faced same kind of issue and I am sharing me experience. Maybe it can help someone.

I did have "Message" NSManagedObject Class.

If we do have properties like:

@NSManaged public var buddyId: Int32
@NSManaged public var message: NSObject?
@NSManaged public var messageServerId: Int32
@NSManaged public var messageUnixTime: Double
@NSManaged public var sender: Int32
@NSManaged public var status: Int32
@NSManaged public var type: Int32
@NSManaged public var userId: Int32
@NSManaged public var buddyName: String?
@NSManaged public var chatroomId: Int32
@NSManaged public var messageSource: Int32

And your predicate is like:

fetchRequest.predicate = NSPredicate(format: "userId == %@ AND buddyId == %@ AND messageSource == %d", userId, buddyId, 10)

So in this case, we do have userId as Int32 but we are passing it as string in predicate. NSFetchedResultsController is on mainContext and all messages(data) processing is happening on privateConext(child context of mainContext).

So NSFetchedResultsController is gonna fetch all messages on performFetch() call but when we do any insert/update/delete operation of message on privateContext, these operations effect will not gonna reflect for NSFetchedResultsController.

The simple reason was because I was not providing proper predicate.

I do need to modify predicate like:

fetchRequest.predicate = NSPredicate(format: "userId == '%@' AND buddyId == '%@' AND messageSource == %d", userId, buddyId, 10)

or I do need to provide predicate like:

fetchRequest.predicate = NSPredicate(format: "userId == %d AND buddyId == %d AND messageSource == %d", userId, buddyId, 10)

Reason was "userId" and "buddyId" was of type Int32 but I was providing them as String in predicate. Hence NSFetchedResultsController was unable to detect message changes.

That's it.

Let me know if someone need a detail discussion about this.

Hope this will help someone.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top