In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "iOS cross-page state synchronization scheme analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "cross-page state synchronization scheme analysis in iOS"!
NotificationCenter
State synchronization is actually an one-to-many scenario, that is, an event can be monitored by multiple observers. The NotificationCenter that comes with Apple's system framework is used to adapt to this scenario, and it is also widely used by the system framework itself and our developers. The usage is as follows:
Define the name of the notification and register the notification based on target-action for key that requires additional information
Open func addObserver (_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)
The method of realizing Monitoring Notification
Func onReceivedNotification (note: NSNotification)
To send a notification, you can pass the object to which the notification was sent (object) and some additional information (userInfo)
Open func post (name aName: NSNotification.Name, object anObject: Any?, userInfo aUserInfo: [AnyHashable: Any]? = nil)
Notice of removal of registration
Open func removeObserver (_ observer: Any, name aName: NSNotification.Name?, object anObject: Any?)
Of course, NotificationCenter also provides a more convenient block-based way to register monitoring notifications, which integrates the two steps of 2p3 into one step.
Open func addObserver (forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, using block: @ escaping (Notification)-> Void)-> NSObjectProtocol
The overall process is clear and easy to use, but it has one serious drawback-weak typing. What we receive is a NSNotification object.
Open class NSNotification: NSObject, NSCopying, NSCoding {open var name: NSNotification.Name {get} open var object: Any? {get} open var userInfo: [AnyHashable: Any]? {get}}
Suppose we need to pass a message concerned about the state change, then we need to include the ID of the person concerned about the changed state as well as the person concerned. Then we need to take the required value out of the userInfo:
Let following = notification.userInfo ["FollowingKey"] as! NSNumberlet userID = notification.userInfo ["UserIDKey"] as! NSNumber
In other words, the party who receives the notification generally needs to look at the document to know how to get a value from userInfo and what type of value is taken. This is extremely inconvenient for use.
SwiftNotificationCenter
SwiftNotificationCenter is a protocol-oriented notification center scheme. The mode of use is as follows:
Define the protocol
Protocol FollowingChanged {func followingDidChange (following: Bool, userID: NSNumber)}
Register notification based on agreement
Broadcaster.register (Update.self, observer: observer)
Implementation protocol method
Extension ViewController: FollowingChanged {func followingDidChange (following: Bool, userID: NSNumber) {/ / do something}}
Send notification
Broadcaster.notify (FollowingChanged.self) {$0.followingDidChange (following, userID)}
Notice of removal of registration
Broadcaster.unregister (Update.self, observer: observer)
We can see that its protocol-based approach solves the problem of weak typing, and it automatically removes notifications through AssociatedObject. But it also has the problem of poor expansibility.
Still focus on the change of the scene, if with the development of the business, some places need to know whether the attention is related to each other, then you need to add a field to identify. So we need to modify the protocol, add parameters, and because it is not a parameter that must be passed, it is an optional type.
Protocol FollowingChanging {func followingDidChange (following: Bool, userID: NSNumber, followingEachOther: NSNumber?)}
If this type of notification is widely used in scenarios, then there is a lot of room for modification. This is obviously unacceptable.
EventBus
EventBus is widely used in Android, and its process is shown in the following figure:
Photo Source: EventBus
The mode of use is as follows:
Define event
Class TPFollowingChangedEvent: NSObject, TPEvent {private (set) var following: Bool private (set) var userID: NSNumber}
Registration event
TPEventBus.shared.register (eventType: TPFollowingChangedEvent.self, subscriber: self, selector: # selector (onEvent (event:object:))
The method of listening for events
Objc func onEvent (event: TPFollowingChangedEvent, object: Any?) {/ / do something}
Send event
TPEventBus.shared.post (event: event, object: self)
Remove the registration of events
TPEventBus.shared.unregister (eventType: TPFollowingChangedEvent.self, subscriber: self)
As we can see, EventBus is also strongly typed.
If you are still focused on the scenario and need to add the followingEachOther parameter, then we just need to add the followingEachOther parameter to the TPFollowingChangedEvent. As follows:
Class TPFollowingChangedEvent: NSObject, TPEvent {private (set) var following: Bool private (set) var userID: NSNumber private (set) var followingEachOther: NSNumber?}
Therefore, the following requirements are implemented using EventBus:
Strongly typed and extensible
Both EventBus and NotificationCenter are based on target-action, but it is not difficult to extend it to support block snooping and also to automatically remove the registration of events. It is used similar to the following:
TPEventBus.shared.subscribe (eventType: TPFollowingChangedEvent.self) .forObject (self) .onQueue (OperationQueue.main). OnEvent {(event, object) in / / do something} .discards (by: self)
Thank you for your reading, the above is the content of "cross-page state synchronization scheme analysis in iOS". After the study of this article, I believe you have a deeper understanding of the problem of cross-page state synchronization scheme analysis in iOS, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.