iOS清空请求队列

标签 ios objective-c networking http-post afhttpclient

当您在我的应用程序中创建/更新/删除记录时,它会在 Core Data 中创建一个 OutgoingRequest。应用程序会定期在队列中获取这些请求,并将它们全部推送到服务器。我使用 AFHTTPClient 帖子(见下文)执行此操作。我遇到的问题是,它会同时推送所有这些请求,然后返回的响应没有真正的顺序。

我需要做的是让这些请求“同步”工作,因为在请求 A 完成(成功或失败响应)之前不应发布请求 B。这也是在后台完成的,不应挂起 UI。

for(OutgoingRequest *req in queue)
{
    NSURL *reqUrl = [NSURL URLWithString: globals.baseURL];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:reqUrl];
    [httpClient setParameterEncoding:AFJSONParameterEncoding];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            req.url, @"viewName",
                            req.json, @"JSON",
                            req.dateAdded.description, @"dateTime",
                            nil];

    NSString *path = [NSString stringWithFormat:@"cache/update/?deviceUID=%@&token=%@", [MySingleton getMacAddress],  globals.token];

    [httpClient postPath:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

            //handle the repsonse

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];
}

有什么方法可以用我当前的设置实现这个目标吗?还是我应该使用其他方式 POST 到我的服务器?

谢谢

最佳答案

这里最好的想法可能是使用 AFHTTPRequestOperation(或其子类之一)并在它们之间创建依赖关系:2 op。取决于 1 op., 3 op.取决于 2 操作。等等……

请查看AFNetworkingNSOperation 文档(AFHTTPRequestOperationNSOperation 的子类)。

关于iOS清空请求队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19729659/

相关文章:

ios - 原型(prototype)单元的自定义 UI 元素无法访问 xcode 6 beta

objective-c - NSViewController 和 xcode 6 中的 acceptsFirstResponder - Swift

ios - 使用带有替换函数的正则表达式替换

ios - iOS 中的后台传输实际上是如何工作的?

ios - Xcode std::__1::basic_ostream undefined symbol

c++ - cv::Scalar 不显示预期的颜色

ios - 将文本水平放入 UIScrollView 和适当的 AutoLayout - Swift

ios - iOS 应用程序启动屏幕中的 UIViewController

linux - 在/etc/sysconfig/network-scripts 中找不到 ifcfg-enp0s3?

多个节点(网络上的计算机)支持的 Java(暴力应用程序)