【问题标题】:ASIHTTPRequest, EXC_BAD_ACCESS when request did finishedASIHTTPRequest, EXC_BAD_ACCESS 当请求完成时
【发布时间】:2010-01-29 20:32:42
【问题描述】:

我正在尝试使用 ASIHTTPRequest 执行异步请求,但在请求完成时收到通知时遇到了一些问题。

-(void)doDownload{
    NSURL *url = [NSURL URLWithString:@"http://www.someurl.com/?"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"someValue" forKey:@"someField"];
    [request setRequestMethod:@"POST"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished)];
    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];

}

requestFinished 永远不会被调用。我在 ASIHTTPRequest.m 中遇到异常,-handleStreamCompleted:

if (fileError) {
    [self failWithError:fileError];
} else {
    [self requestFinished];   <----- this call fails
}

有什么线索吗?

【问题讨论】:

    标签: iphone objective-c delegates selector asihttprequest


    【解决方案1】:

    当请求完成时,您确定实现- (void)requestFinished:(ASIHTTPRequest *)request 的类仍然存在吗?在我看来,这个类被释放得太早了。请注意,delegate 属性不会保留其内容。

    您可以将[self retain] 添加到doDownload 并将[self release] 添加到- (void)requestFinished:(ASIHTTPRequest *)request,但请确保(!)[self release] 不会被太频繁地调用。如果请求永远不会完成,这也是可能的内存泄漏。最好将您的课程保留在其他地方。

    您也可以尝试将NSZombieEnabled 设置为YES 进行调试以查找错误。

    【讨论】:

    • 这似乎是正确的答案。我有一个需要解决的终身管理问题。现在,我创建一个下载类,调用一个方法,然后立即发布。我必须实现一些回调系统,让下载实例所有者知道事情何时完成。无论如何,我最终都需要这个来隐藏进度视图和更新数据等。谢谢。
    • 使用 ARC 运行时如何做到这一点?请看我的topic here
    • 托什。 @tomute 是对的,选择器的名称是 requestFinished:,而不是 requestFinished,这就是整个问题。无需在生命周期和保留等问题上打破你的小脑袋。
    【解决方案2】:

    以下代码行似乎是错误的。

    [request setDidFinishSelector:@selector(requestFinished)];
    

    requestFinished 方法有一个参数 (ASIHTTPRequest *)。
    因此,当您设置如下选择器时,您应该添加 ":"

    [request setDidFinishSelector:@selector(requestFinished:)];
    

    【讨论】:

    • 我相信你的话 ;-) 谢谢。
    【解决方案3】:

    [请求响应字符串];

    在此调用之前检查请求的retainCount。可能它等于零 :) 如果是这样 - 你不应该忘记在 doDownload 方法中创建它时保留它。

    【讨论】:

    • 我无法检查 requestFinished 中的请求 retainCount,因为 requestFinished 永远不会被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多