ASIHTTPRequest的通过结果“requestFinished”返回起源方法 [英] Pass Result of ASIHTTPRequest "requestFinished" Back to Originating Method

查看:193
本文介绍了ASIHTTPRequest的通过结果“requestFinished”返回起源方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法(getAllTeams :)使用ASIHTTPRequest库启动HTTP请求。

  NSURL * httpURL = [[[NSURL alloc] initWithString:@/ api / teamsativeToURL:webServiceURL] autorelease]; 

ASIHTTPRequest * request = [[[ASIHTTPRequest alloc] initWithURL:httpURL] autorelease];
[request setDelegate:self];
[request startAsynchronous];

我想要做的是调用 [WebService getAllTeams ] ,并返回NSArray中的结果。目前,getAllTeams不返回任何内容,因为HTTP响应在requestFinished:方法中求值。



理想情况下,我想要能够调用 [WebService getAllTeams] ,等待响应,它进入一个NSArray。我不想创建属性,因为这是一次性类(意思是它不存储任何值,只检索值),并且多个方法将使用相同的requestFinished(所有的返回一个数组)。

我读了一些代表和NSNotifications,但我不知道,如果他们中的任何一个是最好的方法。我发现这个代码片段通过传递一个选择器作为参数实现回调,但它没有pan out(因为requestFinished独立触发)。



任何建议?



NSArray * teams = [[WebService alloc] getAllTeams]; (目前不工作,因为getAllTeams不返回任何东西,但requestFinished的。我想获得requestFinished的结果,并将它传回getAllTeams:)

解决方案

向调用发送对原始类实例的引用:

  [WebService getAllTeams:self]; 

然后修改 -getAllTeams 到原始实例:

   - (void)getAllTeams:(id)sender {
// ...
[request setDelegate:sender];
// ...
}

您的原始实例将负责处理响应和清理内存。


I have a method (getAllTeams:) that initiates an HTTP request using the ASIHTTPRequest library.

NSURL *httpURL = [[[NSURL alloc] initWithString:@"/api/teams" relativeToURL:webServiceURL] autorelease];

ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:httpURL] autorelease];
[request setDelegate:self];
[request startAsynchronous];

What I'd like to be able to do is call [WebService getAllTeams] and have it return the results in an NSArray. At the moment, getAllTeams doesn't return anything because the HTTP response is evaluated in the requestFinished: method.

Ideally I'd want to be able to call [WebService getAllTeams], wait for the response, and dump it into an NSArray. I don't want to create properties because this is disposable class (meaning it doesn't store any values, just retrieves values), and multiple methods are going to be using the same requestFinished (all of them returning an array).

I've read up a bit on delegates, and NSNotifications, but I'm not sure if either of them are the best approach. I found this snippet about implementing callbacks by passing a selector as a parameter, but it didn't pan out (since requestFinished fires independently).

Any suggestions? I'd appreciate even just to be pointed in the right direction.

NSArray *teams = [[WebService alloc] getAllTeams]; (currently doesn't work, because getAllTeams doesn't return anything, but requestFinished does. I want to get the result of requestFinished and pass it back to getAllTeams:)

解决方案

Send a reference to the originating class instance along with the call:

[WebService getAllTeams:self];

Then modify -getAllTeams to accept a reference to the originating instance:

- (void) getAllTeams:(id)sender {
    // ...
    [request setDelegate:sender];
    // ...
}

Your originating instance will be responsible for handling the response and cleaning up memory.

这篇关于ASIHTTPRequest的通过结果“requestFinished”返回起源方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆