在調用之前釋放了 didFinishSelector ASIHTTPRequest 的自定義委託類 (Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called)


問題描述

在調用之前釋放了 didFinishSelector ASIHTTPRequest 的自定義委託類 (Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called)

I have a separate class I want to handle the response from an ASIHTTPRequest, so I create an instance of the class, then pass it to my request. The problem is that my delegate class is being deallocated before the request finishes.

I'm using ARC. How can I ensure the delegate is retained for the didFinish/didFail selectors?

<pre class="lang‑c prettyprint‑override">‑(void)getStuff
{
    NSURL url = [NSURL URLWithString:@"http://example.com/json"];
    ASIHTTPRequest 
request = [ASIHTTPRequest requestWithURL:url];

    UpdateFeedDelegate *updateFeedDelegate = [[UpdateFeedDelegate alloc] init];

    [request setDelegate:updateFeedDelegate];
    [request setDidFinishSelector:@selector(updateTestDone:)];
    [request setDidFailSelector:@selector(getSingleUpdateFeedBackgroundRequestFailed:)];

    [request startAsynchronous];
}
</code></pre>

In the example, the selector classes exist as expected. I get the error:

*** ‑[UpdateFeedDelegate respondsToSelector:]: message sent to deallocated instance 0x56efb50

*** NSInvocation: warning: object 0x56efb50 of class '_NSZombie_UpdateFeedDelegate' does not implement methodSignatureForSelector: ‑‑ trouble ahead

*** NSInvocation: warning: object 0x56efb50 of class '_NSZombie_UpdateFeedDelegate' does not implement doesNotRecognizeSelector: ‑‑ abort

‑‑‑‑‑

參考解法

方法 1:

Delegates are typically held by weak or assign properties.  You need to hold onto a strong or retain reference to your UpdateFeedDelegate somewhere in order for it to not get autoreleased.  Typically, I hold this in the class that is creating the request and delegate.

(by Tom RedmanDan F)

參考文件

  1. Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called (CC BY‑SA 3.0/4.0)

#asihttprequest #objective-c #automatic-ref-counting #delegates






相關問題

遠程服務器上的請求總是失敗 (Request always fail on distant server)

ASIHTTPRequest 緩存我的數據。我不想要任何類型的緩存 (ASIHTTPRequest caches my data. I don't want any kind of cache)

在調用之前釋放了 didFinishSelector ASIHTTPRequest 的自定義委託類 (Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called)

Android:在後台上傳/下載文件數量的最佳方式 (Android: best way to upload/download number of files in background)

Ghi giá trị bài đăng trong ASIFormDataRequest (Log Post Value in ASIFormDataRequest)

ASIHTTPRequest - 下載問題 (ASIHTTPRequest - download problem)

ASIHTTPRequest 離線模式連接失敗 (ASIHTTPRequest offline mode connection failure)

ASIHttpRequest 請求:didReceiveBytes:未按預期工作 (ASIHttpRequest request:didReceiveBytes: not working as expected)

如何讓 Ruby on Rails 與 ASIHTTPRequest 一起使用? (How do I get Ruby on Rails to work with ASIHTTPRequest?)

可以指定一個 NSObject 作為多個 ASIHTTPRequests 的代表嗎? (OK to assign one NSObject as the delegate of multiple ASIHTTPRequests?)

需要幫助將 NSXMLParser 與 ASIHTTPRequest 一起使用 (need help using NSXMLParser with ASIHTTPRequest)

處理 ASIHTTPRequest 多個請求 (Handling ASIHTTPRequest Multiple Requests)







留言討論