Async data connections iOS

2019-07-19 04:57发布

问题:

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.

回答1:

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.