用 NSURLSession 替换同步 NSURLConnection 的最佳实践 [英] Best practice to replace a synchronous NSURLConnection with NSURLSession

查看:50
本文介绍了用 NSURLSession 替换同步 NSURLConnection 的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为

NSURLConnection sendSynchronousRequest:returningResponse:error:&connectionError

已弃用我将不得不更换我很久以前编写的导入程序.

is set deprecated I will have to replace an Importer I wrote a long time ago.

进口商执行以下操作:

  1. 它从 API-A 获取数据.那里的数据可以在多个页面上.
  2. 它使用来自第一次 fetch(也是多页)的数据从 API-B 查询数据并合并
  3. API-B 查询的结果将与 API-A 的数据合并

我通过后台操作实现了这一点,其中我为每个 API 使用方法,如果请求有多个页面,则递归调用.

I implemented this with a background-operation where I use methods for each API which called recursively if there are mulitple pages for the request.

但由于 NSURLSession 不支持同步请求,我目前只看到有大量开销(例如 iVars)控制完成块中调用的内容(例如下一页或开始查询 API-B)的选项.

But as NSURLSession does not support synchronous request I currently only see the option to have lot of overhead (e.g. iVars) control what's called in the completion block (e.g. next Page or start to query API-B).

那么,将它引入 NSURLSession 的优雅解决方案是什么.

So, what would be an elegant solution to bring this to NSURLSession.

注意:为了确保,我之前的解决方案根本不会阻塞主线程.但在当时,这是控制两个源合并的最简单方法.

NB: Just to make sure, my previous solution does not block the main thread at all. But back then it was the easiest way to control the merge of two sources.

推荐答案

这个答案不应该是最佳实践.这对我来说很实用.

This answer is not supposed to be best practice. It was just practical for me.

面对在后台执行一堆同步请求并且执行顺序很重要的情况,我最终使用了以下内容:

Faced with the situation when a bunch of synchronous requests is executed in background and the order of execution matters I've ended up using the following:

SyncRequestSender.h

SyncRequestSender.h

#import <Foundation/Foundation.h>

@interface SyncRequestSender : NSObject

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request
                 returningResponse:(NSURLResponse **)response
                             error:(NSError **)error;

@end

SyncRequestSender.m

SyncRequestSender.m

#import "SyncRequestSender.h"

@implementation SyncRequestSender

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request
                 returningResponse:(NSURLResponse **)response
                             error:(NSError **)error
{
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);


    NSError __block *err = NULL;
    NSData __block *data;
    NSURLResponse __block *resp;

    [[[NSURLSession sharedSession] dataTaskWithRequest:request
                                     completionHandler:^(NSData* _data, NSURLResponse* _response, NSError* _error) {
        resp = _response;
        err = _error;
        data = _data;
        dispatch_group_leave(group);

    }] resume];

    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

    if (response)
    {
        *response = resp;
    }
    if (error)
    {
        *error = err;
    }

    return data;
}

@end

这篇关于用 NSURLSession 替换同步 NSURLConnection 的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆