Reputation: 641
Both the addObserver(_:selector:name:object:)
and post(name:object:userInfo:)
methods include an "object" parameter. The corresponding documentation describes the object parameter as being, "[t]he object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer," and "[t]he object posting the notification." These descriptions give me the impression that the object "parameter," if not set to nil, must be the same object from which the post()
method is called.
However, in practice, it appears that the post()
method can include any arbitrary object as its object parameter. At the same time, the addObserver()
method can be set with any arbitrary object. Thus, if the post()
and addObserver()
methods refer to the same arbitrary object (and with the same Notification.Name), they should function to send and receive. In experiments, this seems to work just fine.
In the following example, Observer registers to receive a particular notice from sender arbitrary. However, Store is the one that posts the notice. In posting the notice, Store includes the same instant of arbitrary as the object that is the "sender." Observer receives the notice. This works even though Store, rather than arbitrary, is the "sender" of the notice.
class ArbitraryData {
var data: [String] = ["some handy info"]
func getMoreInfo() {
Store.aSyncReadData(self)
}
}
class Observer {
let arbitrary = ArbitraryData()
func initiate() {
NotificationCenter.default.addObserver(self, selector: #selector(handleNotice),
name: NSNotification.Name(rawValue: "eventHappenedNotice"),
object: arbitrary)
... do stuff ...
arbitrary.getMoreInfo()
}
func handleNotice(notice: Notification) {
for infoItem in arbitrary.data {
print(infoItem)
}
}
}
class Store {
static func aSyncReadData(dataObject: ArbitraryData) {
ServerAPI.getSomeDataFromPersistentStore(completionHandler: { newData in
dataObject.data.append(newData)
NotificationCenter.default.post(NSNotification.Name(rawValue: "eventHappenedNotice"),
object: dataObject)
})
}
}
Is this use of an arbitrary object part of the intended functionality, and is it safe? Or, is use of an arbitrary object a kluge that is likely to lead to some kind of failure that may be difficult to debug later?
Upvotes: 0
Views: 313
Reputation: 10175
I think you misunderstood a little bit what you are doing.
First of all in your example, Store
is not the one who posts the notification, it's ArbitraryData
the one who posts the notification.
To explain a little bit more:
If you use the object
parameter when you register/post a notification, the notification will be received/sent only if the object used to register is equal to the object posting the notification. So if you use both name
and object
parameters to register/send notification, name
and object
must be equal in order to have a proper notification communication.
If you don't use the object
parameter, then the only check is applied to name.
So for example, in your case if you use two different instances of ArbitraryData
which are not equal, one for register and one for posting, your notification won't work.
Upvotes: 1