0
仍然讓GCD進入我的腦海,並遇到了一個問題,我希望單個串行隊列能夠跨越我的視圖控制器的生命週期。我想在課堂不同部分的單個隊列上執行幾個塊。全局串行調度隊列
兩個主要的原因是我想避免鎖定,因爲我已經被告知@synchronise不再是最佳實踐,並且有多個獲取請求到我需要同步的互聯網。
我的解決方案是創建一個串行隊列,但它的作用域只在我定義它的方法中。我不想使用全局隊列作爲它的併發隊列,我特別想要一個串行隊列。
@interface MyViewController : UIViewController
{
dispatch_queue_t mySerialQueue;
}
@implementation MyViewController
- (void)viewWillAppear:(BOOL)animated
{
if (!mySerialQueue)
mySerialQueue = dispatch_queue_create("com.myAppQueue", NULL);
}
- (void)myMethod
{
dispatch_async(mySerialQueue, ^{
//My block code here
});
}
- (void)myOtherMethod
{
dispatch_async(mySerialQueue, ^{
//My other block code here
});
}
- (void) viewWillDisappear:(BOOL)animated
{
free(mySerialQueue);
}
此外,當我配置文件測試程序,我沒有得到任何內存泄漏,但如果我刪除「免費(mySerialQueue)我得到一個內存泄露 – timv
什麼是你的問題? – 2011-11-21 03:58:52
很高興聽到你解決它。 – NJones