1

For synchronizing access to resources, I need to use a certain serial queue when checking for a condition which decides if a method should return or not. The rest of the method does not need to be executed on the serial queue, thus I would like to execute it on the calling queue to make the serial queue available for other tasks as quickly as possible. The below code that I'm currently using works fine, but it does not look elegant. I'm simply looking for more elegant code doing the same thing:

- (void) blockInvokingMethod {

    __block BOOL returnConditionResult;

    dispatch_sync(someSerialQueue, ^{
        returnConditionResult = [self isReturnConditionMet];
        if (returnConditionResult) {
            return; // this return should return from the blockInvokingMethod
        }
        [self doOtherStuffRequiringSerialQueue];
    });

    if (returnConditionResult) { // have to check again and return again if true
        return;
    }

    [self doStuffNotRequiringSerialQueue];
}

I have the feeling that I'm doing something in a too complicated way. Maybe this code already implies that my structure of synchronizing access is already too complicated.

Daniel S.
  • 6,458
  • 4
  • 35
  • 78
  • 5
    You code is fine. You need 2 returns because you have 2 different execution contexts. – Wain Nov 27 '13 at 19:30

0 Answers0