Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 当我使用ASIHTTPRequest取消异步web请求时,获得了EXC\u BAD\u访问权限_Iphone_Objective C_Asihttprequest - Fatal编程技术网

Iphone 当我使用ASIHTTPRequest取消异步web请求时,获得了EXC\u BAD\u访问权限

Iphone 当我使用ASIHTTPRequest取消异步web请求时,获得了EXC\u BAD\u访问权限,iphone,objective-c,asihttprequest,Iphone,Objective C,Asihttprequest,我在iPhone模拟器中运行下面的示例代码没有问题,但是当我在iPhone中运行它时,当我调用[asiRequest cancel]时,我总是得到一个EXC_BAD_访问权限。有人能帮忙吗?谢谢 ASIHTTPRequest *asiRequest; -(IBAction)request1{ NSLog(@"request starting"); [self sendRequest]; } -(IBAction)cancel1{ NSLog(@"request cace

我在iPhone模拟器中运行下面的示例代码没有问题,但是当我在iPhone中运行它时,当我调用[asiRequest cancel]时,我总是得到一个EXC_BAD_访问权限。有人能帮忙吗?谢谢

ASIHTTPRequest *asiRequest;

-(IBAction)request1{
    NSLog(@"request starting");
    [self sendRequest];
}
-(IBAction)cancel1{
    NSLog(@"request caceling");
    if(asiRequest)
        [asiRequest cancel];

}

-(void)sendRequest{
    asiRequest=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com/"]];
    [asiRequest setDelegate:self];
    [asiRequest startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"requestFinished");
    asiRequest=nil;
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"request Error=%@",[request error]);
    asiRequest=nil;
}

在检查了api之后,我认为我不应该在requestFinished或requestFailed中发布它

当它完成时,我如何释放它

- (void)cancel
{
    #if DEBUG_REQUEST_STATUS
    NSLog(@"Request cancelled: %@",self);
    #endif
    [[self cancelledLock] lock];

    if ([self isCancelled] || [self complete]) {
        [[self cancelledLock] unlock];
        return;
    }

    [self failWithError:ASIRequestCancelledError];
    [self setComplete:YES];
    [self cancelLoad];
    [[self cancelledLock] unlock];

    // Must tell the operation to cancel after we unlock, as this request might be dealloced and then NSLock will log an error
    [super cancel];
}

您需要保留您的ASIHTTPRequest。因为请求是通过使用方便的构造函数自动释放的,所以您必须保留它,以便它在当前运行循环结束后继续运行


此外,请注意,您不需要在
cancel1:
中检查
asiRequest
是否为
nil
:向nil发送消息不会产生任何不良影响。

我再次更改了代码

它现在运行良好,我想我唯一需要担心的是,当请求意外失败时,我需要在那里释放请求

-(IBAction)request1{
    NSLog(@"request starting");
    [self sendRequest];
}
-(IBAction)cancel1{
    NSLog(@"request caceling");
    if(asiRequest){
        [asiRequest cancel];
        [asiRequest release];
        asiRequest=nil;
    }
}

-(void)sendRequest{

    asiRequest=[[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com/"]] retain];
    [asiRequest setDelegate:self];
    [asiRequest startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"requestFinished");
    [asiRequest release];
    asiRequest=nil;
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"request Error=%@",[request error]);
}

我已经解决了这个问题,只要在requestFinished中释放asiRequest,当它成功时,不要在requestFailed中释放它,而是在其他地方释放它。