代码之家  ›  专栏  ›  技术社区  ›  dpigera

区分API调用?

  •  5
  • dpigera  · 技术社区  · 16 年前

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

    有什么办法吗?

    4 回复  |  直到 16 年前
        1
  •  9
  •   Dan Ray    16 年前

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

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

    在键处为对象设置不同的值 @"type" 在该视图控制器中执行的每个不同的ASIHTTPRequest中,现在可以在中区分它们 -requestFinished: 并妥善处理每一个问题。

    如果你真的喜欢,你可以随身携带任何其他数据,这将是有用的,当请求完成。例如,如果你懒得加载图像,你可以给自己传递一个要填充的UIImageView的句柄,然后在 -requestFinished 加载图像数据之后!

        2
  •  1
  •   Greg    16 年前

    你可以检查一下 request 参数传递给 requestFinished:(ASIHTTPRequest *)request

    例如,如果两个调用具有不同的url,则可以检查 request.url

        3
  •  1
  •   BenMorel Manish Pradhan    12 年前

    您可以设置在创建请求时应调用的相应选择器:

    [request setDelegate: self];
    [request setDidFailSelector: @selector(apiCallDidFail:)];
    [request setDidFinishSelector: @selector(apiCallDidFinish:)];
    

        4
  •  0
  •   Alin    16 年前

    您可以检查url/originalUrl属性,也可以对其进行子类化并添加自己的属性来指示调用的方式,因为比较int比字符串更容易/更快。

    myRequest.callType = FACEBOOK_LOGIN;
    

    枚举中的所有调用如下:

    enum calls {
    FACEBOOK_LOGIN = 101,
    FACEBOOK_GETWALL = 102,
    ...
    }