0

當你使用多線程和只有獲取者時,你需要你的財產是原子嗎?當你使用多線程和只有getter的時候,你需要你的屬性是Atomic嗎?

之前我執行SEGUE,給定的ViewController,我設置實例變量

該屬性將從多個線程訪問(獲取者只有)。這是我正在使用的代碼。正在執行從我的tableView CellForRowAtIndexPath方法。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    Day *aDay = [[[self days] objectAtIndex:indexPath.section] objectAtIndex: indexPath.row]; 

    NSDictionary * data = [OperationsManager workTimeAndAmountForDay: aDay]; 

    NSString *workTime = [data objectForKey:@"Work Time"]; 
    NSString *amount = [data objectForKey:@"Amount"]; 

    NSString *textLabel = [NSString stringWithFormat:@"%d - H: %@ $: %@", [[aDay day_] integerValue], workTime, amount]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 

     [[cell textLabel] setFont: [UIFont systemFontOfSize: 24.0]]; 
     [[cell textLabel] setText: textLabel]; 

     [cell setNeedsLayout]; 

    }); 

}); 

如果我只在一個線程上做這些。 沒有GCD。沒有 erros。但是,如果我這樣做使用GCD這一段時間,我通常會得到這樣的事情,

2012-08-30 13:05:52.027 Work Clock[17236:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'statement is still active' 
*** First throw call stack: 
(0x36a1188f 0x32e28259 0x352c93dd 0x352c8c87 0x3536c951 0x352d8b1f 0x352d0f73 0x35376dd5 0x352e2d1b 0x352e2247 0x352e1d5b 0x352e1c0b 0xc921f 0xc6fed 0x34c7f5cf 0x34c7f55f 0x34c4d495 0x34c42225 0x34c41763 0x34be5f37 0x369701fb 0x32083aa5 0x320836bd 0x32087843 0x3208757f 0x3207f4b9 0x369e5b1b 0x369e3d57 0x369e40b1 0x369674a5 0x3696736d 0x34b2e439 0x34c10cd5 0xbff55 0xbfef0) 
terminate called throwing an exception2012-08-30 13:05:52.032 Work Clock[17236:1b03] *** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x2be320 <x-coredata://E4D2B2E1-B3D9-4358-810B-0FDE7A679A16/Day/p1>'' 
*** First throw call stack: 
(0x36a1188f 0x32e28259 0x352e24f3 0x352e1d5b 0x352e1c0b 0xc95c9 0xc7395 0x37438c59 0x374447bb 0x32f59dfb 0x32f59cd0) 
terminate called throwing an exception(lldb) 

我也有這種方法,

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

它調用一些相同的屬性。有時錯誤發生在那裏,但我只能得到彙編代碼。我目前無法複製錯誤。

一個說明,

我的呼籲,

[OperationsManager workTimeAndAmountForDay: aDay]; 

是一個類方法,在我OperationsManager類中定義。它有沒有類屬性

+(NSDictionary *)workTimeAndAmountForDay:(Day *)aDay; 

所以,如果我使用的是當多個線程我VC,和我的類方法沒有類屬性使用只干將。

爲什麼我不斷收到這些錯誤?而且,他們真的很難複製!它有一些「隨機性」的感覺。

任何幫助最受讚賞!

我可以停止使用GCD。錯誤消失了。但滾動的用戶體驗下降非常大。

預先感謝您。

Nuno

此外,這些是我正在訪問的核心數據對象。不知道這是否有幫助?也只有一個上下文。但話又說回來。我只是在讀物品。不保存或更改對象狀態。

回答

1

關於atomic,讀bbum的帖子在:

http://www.friday.com/bbum/2008/01/13/objectivce-c-atomic-properties-threading-andor-custom-settergetter/

當你看到飛機墜毀,這是相關CoreData。請參閱...

*** Terminating app due to uncaught exception 
'NSObjectInaccessibleException', reason: 'CoreData 
could not fulfill a fault for '0x2be320 
<x-coredata://E4D2B2E1-B3D9-4358-810B-0FDE7A679A16/Day/p1>'' 

...你怎麼訪問CoreData對象,背景,...從多個線程?你讀這篇文章:

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/coredata/Articles/cdConcurrency.html%23//apple_ref/doc/uid/TP40003385-SW1

+0

我居然只得到了我的頭周圍什麼可能寫這篇文章時,已經發生的事情。真的不知道我應該期待什麼。你的鏈接現在都很清楚。我想我今天會研究這些。先生非常感謝您。 – nmdias

+0

不客氣:) – robertvojta

相關問題