异步数据连接iOS [英] Async data connections iOS

查看:67
本文介绍了异步数据连接iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为当前正在使用的应用编写了同步类.

由于大量数据,它首先获得数据计数,然后在NSOperationQueue中分批下载.一切正常,并且我的同步算法可以快速运行.

其工作方式如下...

- (void)synchroniseWithCompletionHandler://block for completion handler
                            errorHandler://block for error handler
{
    [self.queue addOperationWithBlock
     ^{
           //Create an NSURLRequest for the first batch
           //Send the request synchronously
           //Process the result
           //If error then cancel all operations in the queue, run errorHandler and return.
      }];

    [self.queue addOperationWithBlock
     ^{
           //Create an NSURLRequest for the second batch
           //Send the request synchronously
           //Process the result
           //If error then cancel all operations in the queue, run errorHandler and return.
      }];

    //Add all the remaining batches.

    [self.queue addOperationWithBlock
     ^{
           completionHandler();
      }];
}

这可以使内存使用量保持最小,而速度保持最大.想法是下载和进程都在同一个块中,并且都在继续进行队列中的下一个操作之前进行了处理.

除了,我们现在已经在服务器上实现了OAuth2来对呼叫进行身份验证.

我通过通过NXOAuth2库设置NXOAuth2Request来完成此工作.然后设置帐户并提取签名的URL请求.然后像以前一样使用此NSURLRequest.

问题是如果OAuth令牌在同步过程中途过期,则同步失败.

NXOAuth2库具有一个功能...

+ (void)performMethod:(NSString *)aMethod
           onResource:(NSURL *)aResource
      usingParameters:(NSDictionary *)someParameters
          withAccount:(NXOAuth2Account *)anAccount
  sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
      responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;

这通过在刷新令牌后重新发送请求来处理令牌过期的情况.

但是,此功能是异步的,因此我不确定如何最好地将其安装到我的同步程序中.

我可以使用它添加操作,然后将处理放入完成模块中.但这意味着所有下载几乎都可以同时运行,因此无法保证下载的处理顺序(由于数据依赖性,我需要严格按照顺序进行处理.

我现在想到的唯一方法是将它们全部菊花链在一起...

[NXOAuth2Request performFirstRequest...
    {
        deal with the data.
        [NXOauth2Request performSecondRequest...
            {
                deal with the data.
                [NXOauth2Request performThirdRequest...
                    {
                        deal with the data.
                        finish
                     }];
             }];
      }];

这只是一团糟,可能会变得非常混乱.

我还有其他方法可以处理吗?我唯一能想到的就是尝试自己刷新一下.

解决方案

虽然我喜欢块,但是最好使用并发NSOperation完成某些任务.我在github上放了一个非常简单,非常容易采用的项目,它使用的是与我完全相同的文件在商店的我的应用程序中使用,以获取和处理数据.您可以轻松地将相同的策略适应您的任务.

  • 您从进行Web提取的基本并发操作开始,获取一些数据.
  • 通过子类,您可以用不同的方式处理数据
  • 由于它是并发的,这意味着它具有自己的运行循环,因此它可以阻止等待消息,因此您可以在其中合并授权
  • 您可以使用所需的任何逻辑-如果中间出现授权失败,则可以取消url请求,进行授权,然后一次操作即可完成所有操作
  • 您可以链接操作,这样一次就不会有一个活动了

我正在使用这种结构进行所有的Web交互,并且有大约30个子类可以对接收到的数据进行不同类型的处理.

该项目有三个主要类别:

  • OperationsRunner-一个非常小的类,为NSOperationsQueue提供了高级接口

  • ConcurrentOperation-最低要求

  • WebFetcher-运行NSURLConnection并在完成时完成的类

其他子类仅需要提供一种完整"的方法来处理数据.

I've written a synchronisation class for the app I'm currently working on.

Because of the large amount of data it first gets a data count and then batches up the downloads in an NSOperationQueue. This all works fine and I've got the sync algorithm working quickly.

The way it works is as follows...

- (void)synchroniseWithCompletionHandler://block for completion handler
                            errorHandler://block for error handler
{
    [self.queue addOperationWithBlock
     ^{
           //Create an NSURLRequest for the first batch
           //Send the request synchronously
           //Process the result
           //If error then cancel all operations in the queue, run errorHandler and return.
      }];

    [self.queue addOperationWithBlock
     ^{
           //Create an NSURLRequest for the second batch
           //Send the request synchronously
           //Process the result
           //If error then cancel all operations in the queue, run errorHandler and return.
      }];

    //Add all the remaining batches.

    [self.queue addOperationWithBlock
     ^{
           completionHandler();
      }];
}

This works and keeps the memory usage to a minimum and speed to a maximum. The idea is that the download and process are both in the same block and both processed before moving on to the next operation in the queue.

Except, we have now implemented OAuth2 on the server to authenticate the calls.

I have got this working by setting up an NXOAuth2Request through the NXOAuth2 library. Then setting the account and pulling out the signed URL request. Then I use this NSURLRequest as I was doing previously.

The problem with this is that if the OAuth token expires half way through the sync then the sync fails.

The NXOAuth2 library has a function...

+ (void)performMethod:(NSString *)aMethod
           onResource:(NSURL *)aResource
      usingParameters:(NSDictionary *)someParameters
          withAccount:(NXOAuth2Account *)anAccount
  sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
      responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;

This handles the situation of an expired token by resending the request after doing a token refresh.

However, this function is asynchronous so I'm not sure how best to fit it in to my sync program.

I could just add the operations using this and then put the processing into the completion block. But doing this means that all the downloads will pretty much run all at the same time and then there's no way of guaranteeing the order in which the downloads get processed (I need them to be processed in a strict order due to data dependencies.

The only way I can think of doing this now is to daisy-chain them all together...

[NXOAuth2Request performFirstRequest...
    {
        deal with the data.
        [NXOauth2Request performSecondRequest...
            {
                deal with the data.
                [NXOauth2Request performThirdRequest...
                    {
                        deal with the data.
                        finish
                     }];
             }];
      }];

And this is just messy and could get VERY messy.

Is there any other way I could process this at all? The only other thing I can think is to try and do the refreshing myself.

解决方案

While I love blocks there are just some tasks best done with concurrent NSOperations. I put a really simple really easy to adopt project on github, using the EXACT same files I use in my apps in the store, to fetch and process data. You could easily adapt this same strategy to your task.

  • you start with a base concurrent operation that does a web fetch, gets some data.
  • with a subclass you can process that data in different ways
  • since its concurrent, meaning it has its own runloop, it can block waiting for messages so you can incorporate you authorization within it
  • you can use whatever logic you want - if you get an authorization failure in the middle, you could cancel your url request, authorize, then do another all within one operation
  • you can chain operations so that you never have more then one active at a time

I'm using this structure for all my web interactions, and have something like 30 subclasses the do different types of processing on the received data.

The project has three primary classes:

  • OperationsRunner - a really small class that provides a high level interface to NSOperationsQueue

  • ConcurrentOperation - the bare minimum

  • WebFetcher - a class that runs a NSURLConnection and completes when it does

Other subclasses need only supply a "complete" method to process data.

这篇关于异步数据连接iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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