ASIHTTPRequestTester:异步不起作用 [英] ASIHTTPRequestTester: Asynchronous not working
问题描述
我正在尝试使用ASIHTTPRequest为iOS制作一个应用程序,但使用它时遇到了一些问题.为了演示我的问题,我上传了一个测试项目,您可以从此处下载: http://uploads.demaweb.dk/ASIHTTPRequestTester.zip .
I'm trying to make a App for iOS using ASIHTTPRequest, but I'm facing some issues using it. To demonstrate my problems, I've uploaded an test-project, which you can download from here: http://uploads.demaweb.dk/ASIHTTPRequestTester.zip.
我创建了一个使用ASIHTTPRequestDelegate
协议的WebService类:
I've created a WebService class which use the ASIHTTPRequestDelegate
protocol:
#import "WebService.h"
#import "ASIHTTPRequest.h"
@implementation WebService
- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(@"requestFinished");
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSLog(@"requestFailed");
}
- (void) testSynchronous {
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
NSLog(@"starting Synchronous");
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSLog(@"got response");
}
}
- (void) testAsynchronous {
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
NSLog(@"starting Asynchronous");
[request startAsynchronous];
}
@end
同步方法可以正常工作,但是异步方法根本无法工作.首先,从来没有调用requestFinished和requestFailed,现在我正在获取EXC_BAD_ACCESS.这两个测试方法是从我的ViewController的viewDidLoad
调用的.我希望有人能帮助我完成这项工作.
The synchronous method is working fine, but the asynchronous is not working at all. First the requestFinished and requestFailed was never called and right now I'm getting an EXC_BAD_ACCESS. The two test-methods is called from my ViewController's viewDidLoad
. I hope someone can help me making this work.
根据本主题,可能是由于Automatic Reference Counting
为我的项目启用的.添加[self retain]
的主题建议,但是在ARC打开的情况下无法执行此操作.有解决方案的人吗?
As per this topic, it is possible because of the Automatic Reference Counting
which is enabled for my project. The topic advice to add a [self retain]
, but I cannot do this with ARC turned on. Anyone with a solution for this?
根据 MrMage 中的答案进行更新.
Update as per answer from MrMage.
@interface ViewController()
@property (nonatomic,strong) WebService *ws;
@end
@implementation ViewController
@synthesize ws = _ws;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self setWs:[[WebService alloc] init]];
[self.ws testSynchronous];
[self.ws testAsynchronous];
}
@end
推荐答案
您可以将WebService
实例添加为对存在时间足够长的对象的强引用(例如,您的视图控制器),然后告诉该类在完成其工作后删除WebService
(即在requestFinished
和requestFailed
中,您调用视图控制器,告诉它释放WebService
实例).
You could add your WebService
instance as a strong reference to an object that stays around for long enough (say your view controller), then tell that class to get rid of the WebService
after it has done its job (i.e. in requestFinished
and requestFailed
you call back to the view controller, telling it to release the WebService
instance).
这篇关于ASIHTTPRequestTester:异步不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!