【问题标题】:Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called在调用之前释放了 didFinishSelector ASIHTTPRequest 的自定义委托类
【发布时间】:2012-08-03 15:23:00
【问题描述】:

我有一个单独的类,我想处理来自 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

【问题讨论】:

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


    【解决方案1】:

    代表通常由weak 或assign 属性持有。您需要在某处保留 strong 或 retain 对您的 UpdateFeedDelegate 的引用,以使其不会自动释放。通常,我将它保存在创建请求和委托的类中。

    【讨论】:

    • 谢谢。由于我可能一次有很多请求,并且我需要为每个请求使用一个新的委托对象,并且据我所知,默认情况下所有实例变量和局部变量都是强指针,我仍然不确定如何实现这一点。我不能使用强属性,因为我不知道需要多少。
    • 在这种情况下使用NSMutableArray 或NSMutableDictionary,他们强烈引用他们的成员
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多