iPhone ASIHTTP - API调用之间的区别? [英] iPhone ASIHTTP - Distinguishing between API calls?

查看:118
本文介绍了iPhone ASIHTTP - API调用之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个实现ASIHTTP处理API调用视图控制器。

I currently have a view controller that implements ASIHTTP for handling API calls.

我的视图控制器火灾2个独立的电话。我需要能够在-requestFinished(ASIHTT prequest *)请求方法的调用2区分,这样我就可以据此分析每一个...

My view controller fires 2 separate calls. I need to be able to distinguish between the 2 calls in the -requestFinished(ASIHTTPRequest*)request method, so I can parse each one accordingly...

有没有这样做呢?

推荐答案

使用的用户信息领域!这就是它是!

Use the userInfo field! That's what it's for!

这是ASIHTT prequest(或ASIFormDataRequest)对象有一个名为.userInfo财产,可以采取一个NSDictionary与你想要什么。所以我pretty多少总是:

An ASIHTTPRequest (or an ASIFormDataRequest) object has a property called .userInfo that can take an NSDictionary with anything in it you want. So I pretty much always go:

- (void) viewDidLoad { // or wherever
    ASIHTTPRequest *req = [ASIHTTPRequest requestWithUrl:theUrl];
    req.delegate = self;
    req.userInfo = [NSDictionary dictionaryWithObject:@"initialRequest" forKey:@"type"];
    [req startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    if ([[request.userInfo valueForKey:@"type"] isEqualToString:@"initialRequest"]) {
        // I know it's my "initialRequest" .req and not some other one!
        // In here I might parse my JSON that the server replied with, 
        // assemble image URLs, and request them, with a userInfo
        // field containing a dictionary with @"image" for the @"type", for instance.
    }
}

设置不同的值在键的对象 @ pquest你在这个视图控制器做了,你现在可以区分每个不同的ASIHTT $ P $型他们 -requestFinished:,妥善处理他们每个人

Set a different value for the object at key @"type" in each different ASIHTTPRequest you do in this view controller, and you can now distinguish between them in -requestFinished: and handle each of them appropriately.

如果你真的看中了,你可以将它当请求完成,这将是有用的任何其它数据随身携带。举例来说,如果你懒加载图像,你可以通过自己的句柄要填充的UIImageView,然后做,在 -requestFinished 后已经加载的图像数据!

If you're really fancy, you can carry along any other data that would be useful when the request finishes. For instance, if you're lazy-loading images, you can pass yourself a handle to the UIImageView that you want to populate, and then do that in -requestFinished after you've loaded the image data!

这篇关于iPhone ASIHTTP - API调用之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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