OGeek|极客世界-中国程序员成长平台

标题: objective-c - ASIHttpRequest 将异步请求的响应发送回 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:45
标题: objective-c - ASIHttpRequest 将异步请求的响应发送回 View Controller

在我的应用程序中,我有 ViewControllers->Business Logic Layer(BLL)->Data Access Layer(DAL)->(HttpApi) OR (SQLiteApi)

在我的 HttpApi 类中,我将 ASIHttpRequest 用于异步请求。

请求最初来自 ViewController 并一直向上通过上述层。

我希望能够将 Async 请求的结果“注入(inject)”回这个链中,这样它就可以一直返回到 ViewController 并在途中由中间层处理。

我觉得这是我需要的委托(delegate),但我不确定要实现什么。

编辑: 我想我可能需要开发一个从 HTTPAPI 一直返回到 View Controller 的回调链。我需要使用委托(delegate)。

所以, View Controller 符合 BLL 委托(delegate),BLL 符合 DAL 委托(delegate)等等..



Best Answer-推荐答案


我会在收到使用通知的响应后广播请求

[[NSNotificationCenter defaultCenter] postNotificationName"requestFinished" object:request];

那么你的任何一个类都可以听如下:

[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(requestFinished name"requestFinished" object:nil];

- (void)requestFinishedNSNotification*)notification {
    ASIHTTPRequest *request = (ASIHTTPRequest*)[notification object];

    if ([[[request URL] absoluteString] isEqualToString:expectedURL]) {
        NSString *responseString = [request responseString];
        NSLog(@"RESPONSE %@", responseString);
    }
}

关于objective-c - ASIHttpRequest 将异步请求的响应发送回 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676480/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4