我將一個對象從我的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
零?
我不知道,但你傳輸數據聲音的方式** HORRIBLE **。無論如何:你是否嘗試過在'fetchedObjects'屬性中使用'indexOfObject:'方法,所以檢查確定。如果仍然返回零,那麼你的代碼做錯了,對象* *是不同的。 – luk2302
它爲什麼聽起來很可怕?我讓我的單身人士發送一個通知,告知我的其他課程在我的UITableViewCells上更新下載進度。這更新了我的TabBar中的所有視圖,其中使用委託並未跨我的應用更新。有沒有更好的辦法?我會檢查對象是否如此相同 – Siriss
Singleton幾乎每次都是壞的,應該避免幾乎任何代價。也許使用上述代表團或由Objective-C支持KVO。但將數據從A發送到B只是爲了讓它回到A並不像一個強大的設計。 – luk2302