ios - 这种情况下如何使用AFNetworking?

标签 ios afnetworking

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 7 年前

我必须从维护运行服务器信息的主服务器获取各种服务器列表。当某些运行服务器停机时,它将从运行服务器列表中删除。

应用程序启动时必须请求master api获取运行服务器列表。当对运行服务器的某些请求失败时,它必须请求master刷新运行服务器列表。

AFNetworking是异步的,如何让master api的请求在请求运行服务器之前完成?如何优雅地用异步api设计这个逻辑:

1 failed to request to the running servers.
2 request the master to refresh the servers list.
3 try to request the running servers again.

最佳答案

想法是利用 block 或委托(delegate)将更改通知其他类,而不是让线程休眠。因为我对积木非常满意,所以我在这里使用积木。

此处提供的代码仅用于解释概念,复制粘贴可能无效。

做什么:

  1. 我更喜欢为 Web 服务创建单例类,尽管它完全是可选的并且取决于您的编码实践。

  2. 在您将创建的单例模型类中有一个数组来保存当前正在运行的服务器的数组,或者在应用程序委托(delegate)中有一个变量,以便所有类都可以访问它(最好有一个私有(private)变量和为其编写 setter 和 getter 并将它们公开,但随后也由您在 .h 中声明属性)

  3. 在单例类或 appdelegate 中有一个方法将完成 block 作为参数,它将命中主服务器获取所有当前事件的服务器并填充模型类中的数组,然后调用完成 block 。

  4. 在您的 firstViewController 的 ViewDidLoad 或 ViewWillAppear 中,检查数组是否为 nil 如果是,则调用上面声明的方法并在完成时将完成 block 传递给它,您的完成 block 将涉及到它是调用你也会有一组 url。

  5. 每当 url 失败并且服务器似乎已关闭时,再次调用相同的方法并将完成 block 传递给它,重复上述相同的方法。

代码

假设您正在使用 AFNetworking 更新版本并使用 block ,这里是代码。即使您使用委托(delegate)方法也是一样的。

假设您在 appDelegate 中编写了一个名为 getActiveServers 的方法,

-(void)getActiveServers:(void(^)(NSError *)) completionBlock {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager POST:@"http://myURL.com/user" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
         //process the response get the array
        //save it in array variable
        self.activeServerArray = [responseObject valueForKey:@"serverArray"];
        if (completionBlock) {
            completionBlock(nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        completionBlock(error);
    }];
}

在进行 web 服务调用的 yourViewControllers 的 viewDidLoad 中,或在为此而创建 webService 类的任何类的开头,

考虑这里的 ViewController 示例,

- (void)viewDidLoad {
    [super viewDidLoad];
    appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(appDelegate.activeServerArray) {
           //get the url you want and call your method to make webservice call
    }
    else {
         [appdelegate getActiveServers:^(NSError *) {
               if(error) {
                     //show alert fetching active server failed
               }
               else {
                   //call your method to make webservice call
               }
         }];
    }

最后在您发现服务器已关闭时调用 web 服务的方法中,

-(void)myMethodToMakeWebServiceCall {
     //realised server down
     appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
     [appdelegate getActiveServers:^(NSError *) {
           if(error) {
                 //show alert fetching active server failed
           }
           else {
                //call the same method to fetch the data now with updated url
                [self myMethodToMakeWebServiceCall];
           }
     }];
}

关于ios - 这种情况下如何使用AFNetworking?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37518889/

上一篇:ios - nsmutableArray 用最后添加的对象替换元素

下一篇:ios - Storyboard和 customUI 的 drawRect 控制流

相关文章:

ios - 发布到服务器,将 ASIFormDataRequest 替换为 AFnetworking

ios - 我的 iOS 应用程序和 API 之间的合理安全级别是多少

ios - 在 cocos2d 中禁用图层触摸

ios - 应用商店中的应用所有权

ios - 在后台状态卡住http请求

ios - AFNetworking 中适用于 iOS 应用程序崩溃的 AWS 分析

ios - 来自 Afnetworking 的大图像缓存

iphone - 如果应用程序在多任务处理中关闭,有什么方法吗?

ios - 当方向锁定打开且应用程序仅支持纵向时,如何检测设备方向

xcode4 : reliably detect the DerivedData directory of a project/workspace