0
votes

I have a function that has a problem in it. The following line seems to require a delay in order to accurately process whatever data it's processing, but it's not getting that time and and as a result I'm getting the wrong result. The following line:

self.failure = failure

Seems to need about 5-7 seconds to execute, but the problem is that the next line of code is executed before it can finish.

I need to setup self.failure = failure in a way where the rest of the code in the function executes inside a completion block once that line has finished. I'm just not sure how to do it.

I've tried creating a typedef for a completion block, but I have no idea how and where to execute it.

- (void)start:(BMPlaybackStartupProcessCompleteBlock)completion
      failure:(BMPlaybackStartupProcessFailureBlock)failure // what is the error code at the start and end of this method?
    {
        NSParameterAssert(completion);
        NSParameterAssert(failure);

        self.completion = completion;
        self.failure = failure;

        NSOperation *jailBreakOperation = [self jailBreakCheckOperation];
        NSOperation *metadata = [self metadataOperation];
        NSOperation *ads = [self adsOperation];
        NSOperation *mediaPlayer = [self mediaPlayerOperation];
        NSOperation *progress = [self progressTrackerOperation];
        NSOperation *final = [self finalOperation];

        [final addDependency:mediaPlayer];
        [final addDependency:progress];
        if (ads)
        {
            [final addDependency:ads];
            [ads addDependency:metadata];
        }
        [mediaPlayer addDependency:metadata];
        [progress addDependency:metadata];
        [metadata addDependency:jailBreakOperation];

        [self.queue addOperation:final];
        if (ads)
        {
            [self.queue addOperation:ads];
        }
        [self.queue addOperation:mediaPlayer];
        [self.queue addOperation:progress];
        [self.queue addOperation:metadata];
        [self.queue addOperation:jailBreakOperation];

                // INPUTS:
        //  - content ID
        //  - metadata component
        //  - media player component
        //  - ads component
        //  - location manager if required
        //  - auto play?

        // OUTPUTS:
        //  - Possibly an error if not successful
        //  - metadata
        //  - playback details
}

I want self.failure = failure to finish BEFORE the NSOperationQueue stuff executes.

1
You would need to actually call the block. Now you're assigning it to a property, I don't believe that that takes more that a part of a split second. Calling the function would look like failure(); but I'm a bit confused of why you'd want to call the fail block before you did a thing.Julian F. Weinert
I guess that one of the first operations takes a long time. You'd want to call the block in case of a failure or the successblock after everything is done. But keep in mind that your operations won't run asynchroniously, but hard to say what the rest of your code does.Julian F. Weinert

1 Answers

0
votes

NSOperation has completionBlock property.

@property(copy) void (^completionBlock)(void);

So you can perform your operation like this. This will handle failure if occured.

    Create `error` block level property to save error while operation. 

    nsOperatorObject.completionBlock(^{
        if (self.error) {
          self.failure();
        } else {
          self.completion();
        }
    });

This might be wrong answer . Sorry for that :)