How to wait for a thread to finish in Objective-C

前端 未结 7 1947
长情又很酷
长情又很酷 2020-12-23 15:28

I\'m trying to use a method from a class I downloaded somewhere. The method executes in the background while program execution continues. I do not want to allow program exec

相关标签:
7条回答
  • 2020-12-23 16:13

    I'd suggest wrapping up call to the class method in your own method, and set a boolean once it completes. For eg:

    BOOL isThreadRunning = NO;
    - (void)beginThread {   
        isThreadRunning = YES;
    
        [self performSelectorInBackground:@selector(backgroundThread) withObject:nil];
    }
    - (void)backgroundThread {
        [myClass doLongTask];
    
        // Done!
        isThreadRunning = NO;
    }
    - (void)waitForThread {
        if (! isThreadRunning) {
            // Thread completed
            [self doSomething];
        }
    }
    

    How you wish to handle waiting is up to you: Perhaps polling with [NSThread sleepForTimeInterval:1] or similar, or sending self a message each run loop.

    0 讨论(0)
提交回复
热议问题