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.