English 中文(简体)
ASIHTTP 请求 缩略语
原标题:ASIHTTPRequest / ASIFormDataRequest - referencing request object within blocks under ARC

Very similar to this question, I am trying to convert a project that uses ASIHTTPRequest & ASIFormDataRequest to ARC.

在我看来,控制员班级中,我经常提到并使用完成区块中的request物体的特性(参考反应代码、回复数据等):

__block  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:SOME_URL]];    
[request setCompletionBlock:^{   

    if([request responseStatusCode] == 200) ....etc

在改写为非洲排雷中心时,我得到警告:

Capturing request strongly in this block is likely to lead to a retain cycle

如何这样做?

上一个版图中另一个SO用户指出,只要添加__weak<>/code>,就可以在完成栏目之前提出释放请求,我认为这是真实的。

我怎么能够把这些财产适当参照到ARC/ARC之下的完工/休假区?

最佳回答

(我宣读你对另一个问题的评论)

在使用<代码>ASIHTTPRequest实施另外几个模块之后,我得知,最好在提及你的请求时保留<代码>strong<>。 您可以:

self.request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:SOME_URL]];
__weak ASIFormDataRequest *weakRequest = self.request; // __block directive not needed since we only access the instance s properties. 
[self.request setCompletionBlock:^{   

    if([weakRequest responseStatusCode] == 200)
    // ...

这样,你仍然能够控制<代码>。 甚至在你开始申请之后(例如取消申请)。 您可在以下网址上填写: Self.request = nil;, 当您重新准备发布请求时,可在您的填写栏或内填写。 母体反对清理方法。

<>Update:

如果你重新瞄准第5号行动前,则共同点:使用<代码>unsafe_unretained而不是_we。 之所以如此,是因为考虑到<代码>ASIHTTPRequest.m,各栏号为nil,载于其dealloc((即:应否执行)。 虽然我已经进行过检测,但这样,我们就能够保证仍然能够与乌克兰航天局进行测试。

Note:

The only safe way to cancel an ASIHTTPRequest object is to call its clearDelegatesAndCancel method. I ve been bitten by some nasty bugs when I was just using the plain cancel one.

问题回答

If you re targeting iOS versions before 5.0, that do not include weak support:

__unsafe_unretained __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

I ve found this answer to be helpful: https://stackoverflow.com/a/7735770/133875

It says to use __unsafe_unretained as well as __block





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签