Instead of using the common tip, let’s talk about the tip you came across

Add and Remove

  • Multiple additions will receive messages multiple times, so make sure there is a one-to-one correspondence between additions and removals.

Send notifications synchronously

This is similar to KVO in that a notification is not sent until all recipients have received it.

Send notifications and respond to notifications in one thread

This is similar to KVO. NSNotification cannot cross threads: Actions that respond to notifications are by default on the same thread as postNotification. If you want to execute a method that responds to notifications in a specified thread, you can either use the addObserver method with a block or use dispatch_async(XXX).

If you need to cross threads, you can use a notification interface with blocks.

Such as AFN:

dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
            if (self.completionHandler) {
                self.completionHandler(task.response, responseObject, error);
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
            });
        });
Copy the code

To ensure that notifications are sent on the main thread. This can be used to refresh the interface when some network request data arrives.

dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationFilterMaterialRequestFinished object:self userInfo:nil];
    });
Copy the code

Thread safety and a deadlock case

“Object, observer” weak “or” unsafe”

Why is weakSelf used for Object in SDWebImage

SDWebImage changes

FREQUENT CRASH in [SDWebImageDownloaderOperation cancelInternal] #1807

Receives notifications on the specified thread

  1. Use queues and blocks

  2. Using the Mach port

If you want to receive notifications on a specified thread, how do you do that? The apple documentation describes how to do this.

A brief introduction to Mach Port, which is primarily used for interthread communication. In simple terms, the receiving thread registers the NSMachPort. If another thread uses this port to send messages, the registered thread receives the corresponding message and calls handleMachMessage to process it.

Main ideas:

Define an intermediate object NotificationHandler for receiving notifications, including a queue, a thread to receive notifications, a Mach port, and a lock.

First a NotificationHandler registers a notification. The corresponding handler is processNotification, which is called when a post is posted in another thread and does the following. If the notification is received as the specified thread, the message is processed; otherwise, the message is added to the queue and sent to the specified thread via port. Notice that in multithreading, you have to lock the queue.

The specified thread receives the handleMachMessage callback, first removes the notification, then calls processNotification to process it and continues the process.

reference

Notification you may not know

NSNotification Thread management and automatic logout of open source solutions