iPhone ASIHTTP - 区分 API 调用? [英] iPhone ASIHTTP - Distinguishing between API calls?

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

问题描述

我目前有一个视图控制器,它实现了 ASIHTTP 来处理 API 调用.

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

我的视图控制器触发了 2 个单独的调用.我需要能够区分 -requestFinished(ASIHTTPRequest*)request 方法中的 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...

有人这样做吗?

推荐答案

使用 userInfo 字段!这就是它的用途!

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

一个 ASIHTTPRequest(或一个 ASIFormDataRequest)对象有一个名为 .userInfo 的属性,它可以接受一个 NSDictionary,里面有你想要的任何东西.所以我几乎总是去:

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.
    }
}

在您在此视图控制器中执行的每个不同的 ASIHTTPRequest 中,为键 @"type" 处的对象设置不同的值,现在您可以在 -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天全站免登陆