如何使用for循环获取UNNotificationRequest数组以填充SwiftUI列表视图?

问题描述

我想简单地获取已调度的本地通知列表,并使用forEach填充SwiftUI列表。我相信它应该像我在下面所做的那样工作,但是该数组始终为空,因为似乎在for循环完成之前就使用了它。我尝试将getNotifications()函数与完成处理程序一起使用,也将其作为返回函数使用,但两种方法仍然无效。如何等待for循环完成以填充列表?或者,如果还有另一种方法,请告诉我,谢谢。

var notificationArray = [UNNotificationRequest]()

func getNotifications() {
        
print("getNotifications")
        center.getPendingNotificationRequests(completionHandler: { requests in
            for request in requests {
                print(request.content.title)
                notificationArray.append(request)
            }
        })

}

struct ListView: View {

var body: some View {
    
    NavigationView {
    List {
        ForEach(notificationArray,id: \.content) { notification in

            HStack {
                vstack(alignment: .leading,spacing: 10) {
                    let notif = notification.content
                    Text(notif.title)
                    Text(notif.subtitle)
                    .opacity(0.5)
            }
                    }
        
    }

}
 .onAppear() {
        getNotifications()
    }
}
}

更新:

这是我添加通知并再次致电getNotifications的方式。我希望列表随着新数组的创建而动态更新。打印到控制台显示getNotifications正常工作,并且新数组包含添加通知

Section {
                Button(action: {
                    print("Adding Notification: ",title,bodyText,timeIntValue[previewIndex])
                    addNotification(title: title,bodyText: bodyText,timeInt: timeIntValue[previewIndex])
                    showDetail = false
                    self.vm.getNotifications()
                }) {
                    Text("Save Notification")
                }
            }.disabled(title.isEmpty || bodyText.isEmpty)

解决方法

视图未观察到您的全局notificationArray。应该是动态属性...可能的解决方案是将其包装到ObservableObject视图模型中。

这是解决方案的演示:

class ViewModel: ObservableObject {
  @Published var notificationArray = [UNNotificationRequest]()

  func getNotifications() {
        
    print("getNotifications")
        center.getPendingNotificationRequests(completionHandler: { requests in
            var newArray = [UNNotificationRequest]()
            for request in requests {
                print(request.content.title)
                newArray.append(request)
            }
            DispatchQueue.main.async {
              self.notificationArray = newArray
            }
        })
  }
}

struct ListView: View {
  @ObservedObject var vm = ViewModel()
//@StateObject var vm = ViewModel()    // << for SwiftUI 2.0

  var body: some View {
    
    NavigationView {
      List {
        ForEach(vm.notificationArray,id: \.content) { notification in
            HStack {
                VStack(alignment: .leading,spacing: 10) {
                    let notif = notification.content
                    Text(notif.title)
                    Text(notif.subtitle)
                    .opacity(0.5)
                }
            }
      }
   }
   .onAppear() {
        self.vm.getNotifications()
    }
 }
}