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 execution to continue until this method finishes. How do I do that?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How to let a thread communicate with another activ
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Difference between Thread#run and Thread#wakeup?
- Java/Spring MVC: provide request context to child
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Threading in C# , value types and reference types
My first inclination is to not do what you are suggesting. The technique I've used before is to give the thread a selector to a method in the originating object (which is on the main thread). When the second thread is started, the main thread keeps executing, but puts up a busy indicator of some sort on the display. This allows user interaction to continue if required.
When the second thread ends, just before it shuts down, it calls the selector on the main thread. The method that the selector references then removes the busy indicator from the display and tells the main thread to update, picking up whatever data the second thread has generated.
I've used this successfully for an app which accesses a web service (on the second thread) and then updates the display once data is returned without locking it. This makes the user experience much nicer.
Use an NSOperationQueue, like this
(from memory so forgive any minor errors -- you'll get the basic idea):
For such cases I usually using NSCondition class.
Several technical ways to synchronize multi-threads, such as
NSConditionLock
(mutex-lock),NSCondition
(semaphore)。But they are common programming knowledge for other languages(java...) besides objective-c. I prefer to introducerun loop
(special in Cocoa) to implement thread join:I'd suggest wrapping up call to the class method in your own method, and set a boolean once it completes. For eg:
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.
Here's another way to do it using GCD: