0

我想使用 Restkit 异步下载文件。但是,当使用 sendAsynchronously 方法时应用程序崩溃。如果我使用 sendSynchronously 而不是 sendAsynchronously,它不会崩溃。有没有人知道为什么?

这是我的代码

NSString *passURL = @"https://s3.amazonaws.com/mheydt-mhtnd/2012-03-13-1.png";
        RKRequest *request = [RKRequest requestWithURL:[NSURL URLWithString: passURL]];
        request.delegate = self;
        [request sendAsynchronously];


- (void)request:(RKRequest *)request didReceiveResponse:(RKResponse *)response {
    NSLog(@"didReceiveResponse");
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
   NSLog(@"didLoadResponse");   
}

非常感谢!

4

1 回答 1

0

以这种方式使用sendAsynchronously时,请求不会被保留。您最好使用send在实例中添加请求的方法RKRequestQueue(这可以防止请求过早释放):

[request send];

或者,您可以自己强烈引用请求。您的代码将变为:

@interface MyClass
{
    RKRequest *request;
}

我们假设这是一个强有力的参考。在实施中,您将拥有:

@implementation MyClass

- (void)sendTheRequest
{
    NSString *passURL = @"https://s3.amazonaws.com/mheydt-mhtnd/2012-03-13-1.png";
    request = [RKRequest requestWithURL:[NSURL URLWithString: passURL]];
    request.delegate = self;
    [request sendAsynchronously];
}

- (void)request:(RKRequest *)request didReceiveResponse:(RKResponse *)response {
    NSLog(@"didReceiveResponse");
    request = nil;
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    NSLog(@"didLoadResponse");
    request = nil;
}

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error {
    NSLog(@"didFailLoadWithError");
    request = nil;
}

@end
于 2013-04-03T12:53:42.953 回答