36

我維護一個調度隊列作爲我的視圖控制器的屬性。我在我的視圖控制器的init方法中創建了一次這個隊列,併爲一些後臺任務重複使用了幾次。 ARC之前,我這樣做:在ARC之後我應該使用什麼屬性用於調度隊列?

@property (nonatomic, assign) dispatch_queue_t filterMainQueue; 

而且在初始化:

if (filterMainQueue == nil) { 
    filterMainQueue = dispatch_queue_create("com.myQueue.CJFilterMainQueue", NULL); 
} 

但ARC之後,我不知道這是否仍然應該是「分配」,或者它應該是「強「或」弱「。 ARC轉換器腳本沒有改變任何東西,但我不確定是否有一個細微的錯誤來自於這個隊列可能在使用時被釋放的事實?

使用ARC時,3種屬性之間的區別是什麼?對於調度隊列來說最合適的是什麼?

回答

55

更新答案:

在目前的OS X和iOS,分派對象現在都被視爲由ARC的OBJ-C的對象。它們將按照Obj-C對象的相同方式進行內存管理,並且您應該使用strong作爲屬性。

這是由<os/object.h>中定義的OS_OBJECT_USE_OBJC宏控制的。當您的部署目標是OS X 10.8或更高版本或iOS 6.0或更高版本時,它默認設置爲1。如果您正在部署到較舊的操作系統,那麼這將留在0,您應該在下面看到我的原始答案。


原來的答覆:

調度對象(包括隊列)都沒有的OBJ-C的對象,所以唯一可能的選擇是assign。如果您嘗試使用strongweak,編譯器會發出錯誤。 ARC對GCD沒有影響。

+0

@JimThio:[Grand Central Dispatch](http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html) – 2012-05-18 06:16:11

+13

這已不再是真實的山獅 - 請參閱/usr/include/os/object.h。 GCD和XPC「對象」(任何你可能明確發佈的)現在的行爲已經足夠像他們可以參與ARC的ObjC對象。 – jkh 2012-08-28 00:13:04

+0

@jkh是否也包含iOS平臺? – Ankur 2013-01-06 05:29:59

4

這裏是我使用:

@property (readwrite, strong, nonatomic) __attribute__((NSObject)) dispatch_queue_t queue; 
+0

爲什麼?你能解釋一下嗎? – 2013-01-18 13:43:34

+2

它基本上把一個非ObjC對象變成一個ObjC屬性,所以你可以像self.queue – Arvin 2013-01-24 03:51:01

+0

@Arvin那樣容易地使用它。它抱怨__attribute__和刪除它現在似乎工作。我沒有遇到任何問題,所以我猜屬性(讀寫,強,非原子)dispatch_queue_t隊列是安全的嗎? – TheJer 2013-01-30 02:49:39

4

基於iOS7,我測試dispatch_queue對象是否是一個Objective-C對象,我想通了,他們已經是Objective-C的對象。爲了解釋這一點,現在不需要屬性((NSObject))。

1

TL; DR:dispatch_queue_t現在是Objective C對象,可以使用ARC進行管理。

我還沒有測試過這種情況,但使用iOS 7 SDK和Xcode 5,dispatch_queue_t是一種對象類型。我正在宣佈一個隊列屬性爲

@property (nonatomic, strong) dispatch_queue_t syncQueue; 

編譯器很高興,一切都按預期工作。我明確知道,這在iOS 4或5(預ARC爲retain而不是strong)中不起作用。我挖成定義dispatch_queue_t,發現這個:

/*! 
* @typedef dispatch_queue_t 
* 
* @abstract 
* Dispatch queues invoke blocks submitted to them serially in FIFO order. A 
* queue will only invoke one block at a time, but independent queues may each 
* invoke their blocks concurrently with respect to each other. 
* 
* @discussion 
* Dispatch queues are lightweight objects to which blocks may be submitted. 
* The system manages a pool of threads which process dispatch queues and 
* invoke blocks submitted to them. 
* 
* Conceptually a dispatch queue may have its own thread of execution, and 
* interaction between queues is highly asynchronous. 
* 
* Dispatch queues are reference counted via calls to dispatch_retain() and 
* dispatch_release(). Pending blocks submitted to a queue also hold a 
* reference to the queue until they have finished. Once all references to a 
* queue have been released, the queue will be deallocated by the system. 
*/ 
DISPATCH_DECL(dispatch_queue); 

通過的那個聲音,它不應該工作,所以我檢查的DISPATCH_DECL定義和發現這一點,這說明了一切:

/* 
* By default, dispatch objects are declared as Objective-C types when building 
* with an Objective-C compiler. This allows them to participate in ARC, in RR 
* management by the Blocks runtime and in leaks checking by the static 
* analyzer, and enables them to be added to Cocoa collections. 
* See <os/object.h> for details. 
*/ 
7

這裏是一個如何定義dispatch_queue_t屬性爲iOS 6.0和上方和下方的iOS 6.0

#if OS_OBJECT_HAVE_OBJC_SUPPORT == 1 
@property (nonatomic, strong) dispatch_queue_t serialDispatchQueue; 
#else 
@property (nonatomic, assign) dispatch_queue_t serialDispatchQueue; 
#endif 

基本上OS_OBJECT_HAVE_OBJC_SUPPORT我在iOS 6.0及以上版本中定義爲1。 (MAC 10.8及以上)。在iOS 6下面,它被定義爲0.

OS_OBJECT_HAVE_OBJC_SUPPORT定義像GCD這樣的OS對象具有客觀的C支持。因此ARC,內存管理,引用計數等適用於GCD對象。

+1

這不知何故CocoaPods庫失敗http://stackoverflow.com/q/27267865/1049134 – Rivera 2014-12-03 09:27:39

相關問題