iosweb-servicesserial-processing

Serialize webservice calls and callbacks


I have a class that sends webservice calls and delivers the response via delegation. I now want to add a caching layer in between the views and my webservice class. This new class should serialize the calls in a way that every call is delayed until the callback of the previous call has finished.

I tried to realize that with GCD:

- (void)callWebserviceFunctionX {
    dispatch_group_notify(self.serviceGroup, self.serialQueue, ^{
        dispatch_group_enter(self.serviceGroup);

        // call webservice
    });
}

And in the callback:

-(void)callbackFunctionXWithResponse:(id)response {
    // do something

    dispatch_group_leave(self.serviceGroup);
}

The idea to group each call and its callback with dispatch_group_enterand dispatch_group_leave and wait for previous groups using dispatch_group_notify.

However, this approach does not seem to work as I intended. Is there a better way to achieve this?

UPDATE:
I tried every combination of dispatch_group_enter, dispatch_group_leave, dispatch_group_notifyand dispatch_group_async I can think of without success.
I also thought about NSOperationand NSOperationQueue, but - if I understood correctly - that would force me to write a separate class for every webservice call.

Is there another alternative I did not think of yet?


Solution

  • I think you'd be better off using NSOperation, dependencies between them to ensure serialisation and NSOperationQueue to run them.

    To avoid creating a NSOperation subclass for each request you could use the builtin NSBlockOperation, you provide a block to each instance and adding dependencies between the NSBlockOperation instances should give you the aimed serialisation.

    Hope this helps.

    Regards