didFinishSelector的自定义委托类ASIHTTPRequest在被调用之前被释放

时间:2012-08-03 15:23:00

标签: objective-c delegates automatic-ref-counting asihttprequest

我有一个单独的类,我想处理来自ASIHTTPRequest的响应,所以我创建了一个类的实例,然后将它传递给我的请求。问题是我的委托类在请求完成之前被释放。

我正在使用ARC。如何确保为didFinish / didFail选择器保留委托?

-(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];
}

在示例中,选择器类按预期存在。我收到错误:

*** -[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 个答案:

答案 0 :(得分:2)

代表通常由weakassign属性持有。您需要保留strongretain某个地方的UpdateFeedDelegate引用,以便它不会被自动释放。通常,我在创建请求和委托的类中持有此。