How to write the method to execute after completion of two methods (ios)

后端 未结 3 1111
说谎
说谎 2020-12-12 14:29

I have 2 methods to be executed on a button click event say method1: and method2: .Both have network calls and so cannot be sure which one will fin

3条回答
  •  不知归路
    2020-12-12 14:40

    You could just use a flag (aka a BOOL variable) that lets you know in either methods (A or B) if the other one has completed yet. Something on the lines of this:

    - (void) methodA
    {
        // implementation
    
        if (self.didFinishFirstMethod) {
            [self finalMethod];
        } else {
            self.didFinishFirstMethod = YES;
        }
    }
    
    
    - (void) methodB
    {
        // implementation
    
        if (self.didFinishFirstMethod) {
            [self finalMethod];
        } else {
            self.didFinishFirstMethod = YES;
        }
    }
    
    
    - (void) finalMethod
    {
        // implementation
    }
    

    Cheers!

提交回复
热议问题