ASIHTTPRequestTester:异步不起作用

编程入门 行业动态 更新时间:2024-10-26 15:28:34
本文介绍了ASIHTTPRequestTester:异步不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用ASIHTTPRequest为iOS制作一个应用程序,但使用它时遇到了一些问题.为了演示我的问题,我上传了一个测试项目,您可以从此处下载: 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: 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:@"allseeing-i"]; 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:@"allseeing-i"]; 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:异步不起作用

本文发布于:2023-10-23 19:55:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521855.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   ASIHTTPRequestTester

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!