2015-04-06 100 views
0

我將一個對象從我的UITableViewController傳遞給一個單例,然後通過通知返回給我的UITableViewController。 UITableViewController使用NSFetchedResultsController。然後它需要fetchedResultsController中Object的indexPath。indexPathForObject返回零

Object *obj = (Object *)notification.object; 
NSIndexPath *index = [self.fetchedResultsController indexPathForObject:obj]; 

我設置的通知,並在AFNetworking/AFDownloadRequestOperation進步塊:

- (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]; 
} 

有在[self.fetchedResultsController fetchedObjects];只有一個對象,它的傳回通知中的對象相匹配。 index始終爲零,但fetchedResultsController不爲零,[self.fetchedResultsController fetchedObjects]返回正確的對象。爲什麼indexPathForObject零?

+1

我不知道,但你傳輸數據聲音的方式** HORRIBLE **。無論如何:你是否嘗試過在'fetchedObjects'屬性中使用'indexOfObject:'方法,所以檢查確定。如果仍然返回零,那麼你的代碼做錯了,對象* *是不同的。 – luk2302

+0

它爲什麼聽起來很可怕?我讓我的單身人士發送一個通知,告知我的其他課程在我的UITableViewCells上更新下載進度。這更新了我的TabBar中的所有視圖,其中使用委託並未跨我的應用更新。有沒有更好的辦法?我會檢查對象是否如此相同 – Siriss

+0

Singleton幾乎每次都是壞的,應該避免幾乎任何代價。也許使用上述代表團或由Objective-C支持KVO。但將數據從A發送到B只是爲了讓它回到A並不像一個強大的設計。 – luk2302

回答

0

在我看來就像你正試圖找到一個對象從通知對象表視圖...

你應該調用索引路徑方法,你的表視圖對象,而不是您的通知對象。

試試你的局部變量匹配:

Object *obj = (Object *)notification.object; 

有:

[self.fetchedResultsController fetchedObjects]; 

(如果它是唯一一個應該是比較容易)...

NSArray *arrayFetchedObjects = [self.fetchedResultsController fetchedObjects]; 
Object *tableObject = [arrayObjects lastObject]; 

(如果有多個,那麼你可能需要過濾arrayFetchedObjects,但這是另一個問題)

然後定位該取出對象的表中的視圖中的位置:

NSIndexPath *indexPath = [self.fetchedResultsController indexPathForObject:tableObject]; 
+1

這並沒有解決它。我想出了這個問題,這與我的分類有關。該對象的索引爲零,因爲它被放置在未命名的部分中。直到我開始搞這些事情之前,我沒有看到錯誤。 – Siriss

2

我有同樣的問題。在我的NSFetchedResultsController的配置中,sectionNameKeyPath被設置爲指代(可選)相關對象。 事實證明,這種關係必須得到滿足(即相關對象不能爲零)。 確保sectionNameKeyPath的每個關係都滿足後,indexPathForObject方法再次按預期工作。