2016-12-03 27 views
1

我們有一個覆蓋drawSelectionInRect的NSTableRowView的子類:(它由drawRect內部調用:)。這個方法需要訪問NSManagedObjectContext,所以作爲一個完整的檢查,我們有一個斷言,我們在主線程中。現在事實證明,有時Apple會從非主線程調用drawRect(下面的示例堆棧跟蹤)。這是否意味着它甚至可以從兩個不同的線程同時進行?即在覆蓋drawRect時,我們必須自己考慮線程安全:並使用數據模型?Mac OS是否調用drawRect等顯示方法:來自不同線程的併發?訪問數據模型時沒有那麼危險嗎?

 
ERROR: Assertion failed: [NSThread isMainThread]: (ZS_AppDelegate.m:1287) 
(
    0 tomedo_kunden      0x00000001008af46a -[ZS_AppDelegate loggedInUser] + 554 
    1 tomedo_kunden      0x00000001016feca7 +[Nutzer loggedInUser] + 71 
    2 tomedo_kunden      0x0000000100f33397 +[ZSUserDefaults(MacOS) colorFromPreferences:] + 167 
    3 tomedo_kunden      0x00000001003d8265 -[Besuch(BesuchCategory) baseTextColor] + 101 
    4 tomedo_kunden      0x000000010071f846 -[ZSTableRowView drawSelectionInRect:] + 806 
    5 AppKit        0x00007fffd189d26c -[NSTableRowView drawRect:] + 242 
    6 AppKit        0x00007fffd184eba3 -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] + 1318 
    7 AppKit        0x00007fffd184f053 -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] + 2518 
    8 AppKit        0x00007fffd184e548 __46-[NSView(NSLayerKitGlue) drawLayer:inContext:]_block_invoke + 267 
    9 AppKit        0x00007fffd184e071 -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 1589 
    10 AppKit        0x00007fffd184da36 -[NSView(NSLayerKitGlue) drawLayer:inContext:] + 80 
    11 AppKit        0x00007fffd19839ff -[_NSBackingLayerContents drawLayer:inContext:] + 162 
    12 QuartzCore       0x00007fffd96a1e42 -[CALayer drawInContext:] + 257 
    13 AppKit        0x00007fffd19834d8 -[_NSTiledLayer drawTile:inContext:] + 624 
    14 AppKit        0x00007fffd198320e -[_NSTiledLayerContents drawLayer:inContext:] + 176 
    15 QuartzCore       0x00007fffd96a1e42 -[CALayer drawInContext:] + 257 
    16 AppKit        0x00007fffd1983155 -[NSTileLayer drawInContext:] + 169 
    17 QuartzCore       0x00007fffd9584a38 CABackingStoreUpdate_ + 3740 
    18 QuartzCore       0x00007fffd96a1a3c ___ZN2CA5Layer8display_Ev_block_invoke + 75 
    19 QuartzCore       0x00007fffd96a169d _ZN2CA5Layer8display_Ev + 1803 
    20 AppKit        0x00007fffd198306c -[NSTileLayer display] + 119 
    21 QuartzCore       0x00007fffd9695546 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 572 
    22 QuartzCore       0x00007fffd9695671 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 35 
    23 QuartzCore       0x00007fffd968ae88 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280 
    24 QuartzCore       0x00007fffd9581d55 _ZN2CA11Transaction6commitEv + 475 
    25 AppKit        0x00007fffd19701d9 __58-[_NSScrollingConcurrentVBLMonitor _updateScrollAnimation]_block_invoke + 972 
    26 AppKit        0x00007fffd196aa3a -[_NSScrollingConcurrentSharedData threadSafePropertyAccess:] + 44 
    27 AppKit        0x00007fffd196d127 -[_NSScrollingConcurrentVBLMonitor _updateScrollAnimation] + 672 
    28 AppKit        0x00007fffd196c84e -[_NSScrollingConcurrentVBLMonitor _synchronizeForVBLSerialNumber:timestamp:updateDuration:] + 561 
    29 AppKit        0x00007fffd196c5ad __42-[_NSScrollingConcurrentVBLMonitor resume]_block_invoke + 218 
    30 AppKit        0x00007fffd196c4ca __45-[NSScreen(NSScreenUpdate) addUpdateHandler:]_block_invoke_2 + 212 
    31 libdispatch.dylib     0x000000010345ffcc _dispatch_client_callout + 8 
    32 libdispatch.dylib     0x0000000103476bca _dispatch_continuation_pop + 1025 
    33 libdispatch.dylib     0x000000010346c6bc _dispatch_source_latch_and_call + 195 
    34 libdispatch.dylib     0x0000000103462e15 _dispatch_source_invoke + 1106 
    35 libdispatch.dylib     0x00000001034768f0 _dispatch_continuation_pop + 295 
    36 libdispatch.dylib     0x000000010346deda _dispatch_async_redirect_invoke + 777 
    37 libdispatch.dylib     0x0000000103462247 _dispatch_root_queue_drain + 671 
    38 libdispatch.dylib     0x0000000103461f58 _dispatch_worker_thread3 + 114 
    39 libsystem_pthread.dylib    0x00000001034d78c2 _pthread_wqthread + 1299 
    40 libsystem_pthread.dylib    0x00000001034d739d start_wqthread + 13 
) 
+4

如果你直接從drawRect中訪問CoreData,你應該修改你的架構。 –

回答

4

的NSView子類可以在最近的OS X版本的同時得出這樣的行爲可以通過覆蓋canDrawConcurrently並返回NO被關閉。

您可能會發現最好將視圖與模型分開一點,並將要渲染的數據與CoreData管理的數據分離一點;將要呈現的數據拉入僅限本地的表示形式,並讓AppKit同時更新視圖。

https://developer.apple.com/reference/appkit/nsview/1483425-candrawconcurrently

相關問題