久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

ARC、塊和保留周期

ARC, Blocks and Retain Cycles(ARC、塊和保留周期)
本文介紹了ARC、塊和保留周期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

使用 ARC 處理面向 4.0 和 5.0 的 iOS 項目.

Working on an iOS project that targets 4.0 and 5.0, using ARC.

遇到與塊、ARC 和從塊外引用對象相關的問題.這是一些代碼:

Running into an issue related to blocks, ARC and referencing an object from outside the block. Here's some code:

 __block AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
   [operation setCompletionBlock:^ {
       if ([operation isCancelled]) {
           return;
       }

... do stuff ...

operation = nil;
}];

在這種情況下,編譯器會發(fā)出警告,在塊中使用操作"將導致保留循環(huán).在 ARC 下,__block 現(xiàn)在保留了變量.

In this case, the compiler gives a warning that using 'operation' in the block is going to lead to a retain cycle. Under ARC, __block now retains the variable.

如果我添加 __unsafe_unretained,編譯器會立即釋放該對象,所以顯然這不起作用.

If I add __unsafe_unretained, the compiler releases the object immediately, so obviously that won't work.

我的目標是 4.0,所以我不能使用 __weak.

I'm targeting 4.0 so I can't use __weak.

我試著做這樣的事情:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
__block __unsafe_unretained AFHTTPRequestOperation *weakOperation = operation;

但是雖然 weakOperation 不是 nil,但它的任何屬性都不會在塊內(nèi)填充.

but while weakOperation isn't nil, none of it's properties are populated when inside the block.

鑒于上面列出的項目限制,處理這種情況的最佳方法是什么?

What's the best way to handle this situation given the project constraints listed above?

推薦答案

假設進度保證,保留周期可能正是您想要的.您在塊結束時顯式中斷保留循環(huán),因此它不是永久保留循環(huán):當調(diào)用塊時,循環(huán)被中斷.

Assuming progress guarantees, a retain cycle might be exactly what you want. You explicitly break the retain cycle at the end of the block, so it's not a permanent retain cycle: when the block is called, the cycle is broken.

但是,如果您有其他東西保持操作,您可以將引用存儲到 __weak__unsafe_unretained 變量中,然后在您的塊中使用它.除非您出于某種原因需要在塊期間更改變量的綁定,否則無需對變量進行 __block 限定;由于您不再需要中斷循環(huán),因此您不需要為弱變量分配任何內(nèi)容.

If you have something else keeping the operation around, though, you can store a reference into either a __weak or __unsafe_unretained variable and then use that from within your block. There's no need to __block-qualify the variable unless you for some reason need to change the variable's binding during the block; since you don't have a retain cycle to break any more, you shouldn't need to assign anything to the weak variable.

這篇關于ARC、塊和保留周期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Using Instruments to test an iOS app without having source code to the application(在沒有應用程序源代碼的情況下使用 Instruments 測試 iOS 應用程序)
KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動運行/壓力測試 iOS 應用程序以找出罕見 UI 錯誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態(tài)和動態(tài)單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級到 Xcode 4.5 后,iOS 5 SDK 消失了)
主站蜘蛛池模板: 亚洲精品一区二区三区在线 | 国产综合久久 | 成人av一区二区在线观看 | 欧美成人一区二区 | 国产一区二区毛片 | 国产丝袜一区二区三区免费视频 | 亚洲精品区 | 91青青草视频 | 国产无人区一区二区三区 | 精品国产乱码久久久久久闺蜜 | 成人免费在线观看视频 | 欧美福利在线 | xxxxx黄色片 欧美一区免费 | 精品一区二区三区在线视频 | 亚洲一区二区三区视频免费观看 | 国产91丝袜在线播放 | 日韩有码一区二区三区 | 干干干操操操 | 韩国理论电影在线 | 欧美成年黄网站色视频 | 欧美精品一区在线发布 | 一区中文 | 午夜专区 | 国产福利一区二区 | 亚洲精品久久久一区二区三区 | 日韩精品一区二区三区视频播放 | 欧美成人一区二免费视频软件 | 欧美成年黄网站色视频 | 中文字幕在线一区二区三区 | 日本欧美国产在线 | 在线一区视频 | 日韩a在线观看 | 在线婷婷| 成人免费视频网站在线看 | 天天澡天天狠天天天做 | 精品国产亚洲一区二区三区大结局 | 免费看的黄网站 | 中文字幕av亚洲精品一部二部 | 亚洲社区在线 | 91精品国产91久久久久久三级 | 香蕉视频1024 |