Reputation: 3777
I am passing an object from my UITableViewController to a singleton, then back to my UITableViewController through a notification. The UITableViewController uses a NSFetchedResultsController. It then want the indexPath of the Object in the fetchedResultsController.
Object *obj = (Object *)notification.object;
NSIndexPath *index = [self.fetchedResultsController indexPathForObject:obj];
I set the notification in the AFNetworking/AFDownloadRequestOperation progress block:
- (void)downloadObject:(Object *)object
{
........
[operation1 setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
////// Sending Notification
////// Try to send notification only on significant download
totalBytesRead = ((totalBytesRead *100)/totalBytesExpectedToReadForFile);
NSLog(@"TOTAL BYTES IN PROGRESS BLOCK: %llu",totalBytesRead);
NSMutableDictionary *userDictionary = [[NSMutableDictionary alloc]init];
[userDictionary setObject:[NSNumber numberWithLongLong:totalBytesRead] forKey:@"progress"];
[userDictionary setObject:[NSNumber numberWithLongLong:totalBytesExpectedToReadForFile] forKey:@"totalBytes"];
[[NSNotificationCenter defaultCenter] postNotificationName:[NSString stringWithFormat:@"DownloadProgress%@",object.uniqueID] object:object userInfo:userDictionary];
}
There is only one object in [self.fetchedResultsController fetchedObjects];
and it matches the object the passed back in the notification. The index
is always nil, but the fetchedResultsController is not nil, and the [self.fetchedResultsController fetchedObjects]
returns the correct object. Why does indexPathForObject
nil?
Upvotes: 1
Views: 753
Reputation: 139
I had the same problem. In the configuration of my NSFetchedResultsController
the sectionNameKeyPath
was set to refer to an (optional) related object.
It turned out that this relation must be fulfilled (that is the related object must not be nil).
After ensuring, that every relation of the sectionNameKeyPath
is fulfilled, the indexPathForObject
method worked again as expected.
Upvotes: 2
Reputation: 3799
Looks to me like you are attempting to locate an object in a table view from the notification object...
You should be calling the index path method on your table view object, not your notification object.
Try matching your local variable:
Object *obj = (Object *)notification.object;
with:
[self.fetchedResultsController fetchedObjects];
(if it is the only one that should be relatively easy)...
NSArray *arrayFetchedObjects = [self.fetchedResultsController fetchedObjects];
Object *tableObject = [arrayObjects lastObject];
(if there is more than one then you might need to filter the arrayFetchedObjects
but that is another question)
then locating the position of that fetched object in the table view:
NSIndexPath *indexPath = [self.fetchedResultsController indexPathForObject:tableObject];
Upvotes: 0