gpt4 book ai didi

ios - 在 SwiftUI 中暂停通知发布者

转载 作者:行者123 更新时间:2023-11-28 13:24:06 27 4
gpt4 key购买 nike

当我调用后端服务(登录、值检查……)时,我在相关 View 上使用通知发布者来异步管理更新。我想在 View 消失时取消订阅通知,或者“暂停”发布者。我首先使用 WWDC19 Combine 和相关 SwiftUI 演讲中的简单“分配”选项,然后我查看了这个 great post和 onReceive 修饰符。但是,即使 View 不可见, View 也会不断更新发布的值。

我的问题是:

  1. 我可以在 View 不可见时“暂停”此发布者吗?
  2. 我真的应该担心这个吗,它会影响资源吗(后端更新可能会触发列表和图像显示的大刷新)还是我应该让 SwiftUI 在后台进行管理?

示例代码:选项 1:onReceive

struct ContentView: View {

@State var info:String = "???"
let provider = DataProvider() // Local for demo purpose, use another pattern

let publisher = NotificationCenter.default.publisher(for: DataProvider.updated)
.map { notification in
return notification.userInfo?["data"] as! String
}
.receive(on: RunLoop.main)

var body: some View {
TabView {
VStack {
Text("Info: \(info)")
Button(action: {
self.provider.startNotifications()
}) {
Text("Start notifications")
}
}
.onReceive(publisher) { (payload) in
self.info = payload
}
.tabItem {
Image(systemName: "1.circle")
Text("Notifications")
}
VStack {
Text("AnotherView")
}
.tabItem {
Image(systemName: "2.circle")
Text("Nothing")
}
}
}
}

选项 2:onAppear/onDisappear

struct ContentView: View {

@State var info:String = "???"
let provider = DataProvider() // Local for demo purpose, use another pattern

@State var cancel: AnyCancellable? = nil

var body: some View {
TabView {
VStack {
Text("Info: \(info)")
Button(action: {
self.provider.startNotifications()
}) {
Text("Start notifications")
}
}
.onAppear(perform: subscribeToNotifications)
.onDisappear(perform: unsubscribeToNotifications)
.tabItem {
Image(systemName: "1.circle")
Text("Notifications")
}
VStack {
Text("AnotherView")
}
.tabItem {
Image(systemName: "2.circle")
Text("Nothing")
}
}
}

private func subscribeToNotifications() {
// publisher to emit events when the default NotificationCenter broadcasts the notification
let publisher = NotificationCenter.default.publisher(for: DataProvider.updated)
.map { notification in
return notification.userInfo?["data"] as! String
}
.receive(on: RunLoop.main)

// keep reference to Cancellable, and assign String value to property
cancel = publisher.assign(to: \.info, on: self)
}

private func unsubscribeToNotifications() {
guard cancel != nil else {
return
}
cancel?.cancel()
}
}

对于这个测试,我使用了一个虚拟服务:

class DataProvider {   
static let updated = Notification.Name("Updated")
var payload = "nothing"
private var running = true

func fetchSomeData() {
payload = Date().description
print("DEBUG new payload : \(payload)")
let dictionary = ["data":payload] // key 'data' provides payload
NotificationCenter.default.post(name: DataProvider.updated, object: self, userInfo: dictionary)
}

func startNotifications() {
running = true
runNotification()
}

private func runNotification() {
if self.running {
self.fetchSomeData()
let soon = DispatchTime.now().advanced(by: DispatchTimeInterval.seconds(3))
DispatchQueue.main.asyncAfter(deadline: soon) {
self.runNotification()
}
} else {
print("DEBUG runNotification will no longer run")
}
}

func stopNotifications() {
running = false
}
}

最佳答案

您的程序中似乎有两个发布者名称let publisher。请删除其中一组。 self.info = payloadpublisher.assign(to:\.info, on: self)} 也在复制。

               }
.onAppear(perform: subscribeToNotifications)
.onDisappear(perform: unsubscribeToNotifications)
.onReceive(publisher) { (payload) in
// self.info = payload
print(payload)
}
.tabItem {

在以下内容中:

                  @State var cancel: AnyCancellable? = nil

private func subscribeToNotifications() {
// publisher to emit events when the default NotificationCenter broadcasts the notification
// let publisher = NotificationCenter.default.publisher(for: DataProvider.updated)
// .map { notification in
// return notification.userInfo?["data"] as! String
// }
// .receive(on: RunLoop.main)

// keep reference to Cancellable, and assign String value to property
if cancel == nil{
cancel = publisher.assign(to: \.info, on: self)}

}

private func unsubscribeToNotifications() {
guard cancel != nil else {
return
}
cancel?.cancel()
}

现在您可以看到,cancel?.cancel() 确实有效并且 info 标签在您从 tab2 返回后不再更新。 ~~~发布者在这里暂停,因为订阅已被取消。~~~

发布者没有暂停,因为 View 中有另一个订阅者,所以 print(payload) 仍然有效。

关于ios - 在 SwiftUI 中暂停通知发布者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673424/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com