[SWIFT] A story that did not work when trying to handle events in Notification Center

Introduction

I tried to process notifications between classes using NotificationCenter, but it didn't work, so I'll leave a note of it. Since it is made to move by copy and paste, you can paste it on Playground etc. and check it.

If it doesn't work

Introducing the case that does not work first. Roughly speaking, the Sender class posts the notification keyword test when it is instantiated. The Receiver class will receive the notification keyword test when instantiated, and will execute test () when it does.

import Foundation
class Sender {
    let notificationCenter = NotificationCenter()
    init() {
        notificationCenter.post(name: NSNotification.Name(rawValue: "test"),
                                        object: nil)
    }
}

class Receiver {
    let notificationCenter = NotificationCenter()
    init() {
        notificationCenter.addObserver(forName: .init(rawValue: "test"),
                                               object: nil,
                                               queue: nil,
                                               using: { [unowned self] notification in
                                                test()
                                               })
        }
    func test() {
        print("notification")
    }
}

let receiver = Receiver()
let sender = Sender()

If it works

Here's when it works. The difference from if it didn't work is that you are using an instantiation of NotificationCenter. notificationCenterNotificationCenter.default

import Foundation
class Sender {
    init() {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "test"),
                                        object: nil)
    }
}

class Receiver {
    init() {
        NotificationCenter.default.addObserver(forName: .init(rawValue: "test"),
                                               object: nil,
                                               queue: nil,
                                               using: { [unowned self] notification in
                                                test()
                                               })
        }
    func test() {
        print("notification")
    }
}

let receiver = Receiver()
let sender = Sender()

Cause

The cause was that each class instantiated and used NotificationCenter. It's natural that you can't do it because you don't use it across classes.

bonus

Still want to use the instantiated one! I will describe what to do in that case. It may be a rare case, but I think that this idea can be used in AVAudioPlayer etc. Maybe this is more common.

import Foundation
class Sender {
    let notificationCenter = NotificationCenter()
    func send() {
        notificationCenter.post(name: NSNotification.Name(rawValue: "test"),
                                        object: nil)
    }
}

class Receiver {
    let notificationCenter: NotificationCenter
    init(initNC : NotificationCenter) {
        self.notificationCenter = initNC
        notificationCenter.addObserver(forName: .init(rawValue: "test"),
                                               object: nil,
                                               queue: nil,
                                               using: { [unowned self] notification in
                                                test()
                                               })
        }
    func test() {
        print("notification")
    }
}

let sender = Sender()
let receiver = Receiver(initNC: sender.notificationCenter)
sender.send()//notification

I changed a part of the source code when it didn't work as follows.

--Changed to send the notification of Sender class bysend ()instead ofinit (). --Defined a variable of type NotificationCenter in the Receiver class.

By doing this, you can use a variable of type NotificationCenter that is common between classes.

Recommended Posts

A story that did not work when trying to handle events in Notification Center
The story that did not disappear when I tried to delete mysql on ubuntu
[Gradle] The story that the class file did not exist in the jar file
Handling when calling a key that does not exist in hash
The story that the build error did not stop when using Eclipse 2020
The story of introducing Gradle as a retrofit to an existing system that did not manage packages
Solution that gives an error when trying to connect to DB (MySQL) in Java
A story that I was really into when I did triple DES with ruby
About the matter that the code to read the C structure member (Char array) that was working in swift 2.3 in swift 3 did not work
A story that took time to establish a connection
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment
A story about trying to operate JAVA File
A story that people who did iOS solidly may be addicted to the implementation of Listener when moving to Android
A story that stumbled when deploying a web application created with Spring Boot to EC2
A story I was addicted to when getting a key that was automatically tried on MyBatis
A story that I had a hard time trying to build PHP 7.4 on GCE's CentOS 8
A small story that is sometimes useful in Maven
A memorandum when trying to create a GUI using JavaFX
A story about trying to get along with Mockito
A story about trying hard to decompile JAR files
The story that led to solving the error because postgres did not start with docker-compose up
A note that I had trouble when trying to use nginx with Remote-Containers of vscode
What to do when a could not find driver appears when connecting to a DB in a Docker environment
A story that I realized that I had to study as an engineer in the first place