iOS Are Methods Called by Delegates and Observers Executed on the Main Thread

iOS Are methods called by delegates and observers executed on the main thread?

For delegates this can vary. If the documentation does not specify, then usually they are sent on the main thread. Traditionally UIKit must be used on the main thread so those delegates will almost always be called from the main thread.

For notifications I think you want this little snip.

A notification center delivers notifications to observers synchronously. In other words, the postNotification: methods do not return until all observers have received and processed the notification. To send notifications asynchronously use NSNotificationQueue. In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

From http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html

And finally for KVO, the notifications can come in from other threads. Here is what an Apple Engineer had to say about handling them.

http://lists.apple.com/archives/cocoa-dev/2007/May/msg00022.html

React Native and Objective C delegates

I know this isn’t an an answer to the post but for the bit where you’ve asked for a more technical understanding - dispatch_get_main_queue(); puts the delegate method responses on to the main thread. Since JS is single threaded any process on the background thread won’t be visible to it.

audioRecorderBeginInterruption not called

Ok, so I found the solution to my problem. The issue is that I have an AVCaptureSession elsewhere in the app. In cases like this, it is important to let the capture session instance know that it is not to use the application's audio session. This is done simply like so:

    self.captureSession.usesApplicationAudioSession = NO;

Now, your AVAudioRecorderDelegate will handle the interruptions (or, if you choose to use notifications, they will work as well).

Use of delegates in NSOperation

Its going to call the delegate method in the same operation queue as main is running in. And NSOperation queues are serial by default. Your while loop is just spinning forever (because the operation is never cancelled) and the call to your delegate method is sitting in the queue behind it never able to run.

Get rid of the while loop entirely and let the operation finish. Then when the delegate method is called, if it's cancelled discard the result by returning.

-[UIApplication delegate] must be called from main thread only

Just call it from the main thread like this.

Objective-C

dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication delegate] fooBar];
});

Swift

DispatchQueue.main.async {
YourUIControlMethod()
}

Reaching out to your app delegate like this, is a hint that your architecture could use a little cleanup.

You can call delegates from any thread you want. You only need to make sure you're on the main thread for UIKit calls.
Or that you're on the correct thread your CoreData objects expect. It all depends on the API contract your objects have.



Related Topics



Leave a reply



Submit