我从我的api中获取数据并为它们创建一个类。我可以使用swifyJSON来正确地初始化它们。问题是,当我把我的observedObject放在一个列表中时,它只能正确显示一次。在我更改视图后,它将崩溃。(这个视图在tabView中)有人知道我的getAllNotification()应该把view.onAppear()或List.onAppear()放在哪里吗?谢谢!!
class ManagerNotification : Identifiable, ObservableObject{
@Published var id = UUID()
var notifyId : Int = 0
var requestId : Int = 0
var requestName: String = ""
var groupName : String = ""
// var imageName: String { return name }
init(jsonData:JSON) {
notifyId = jsonData["notifyId"].intValue
requestId = jsonData["requestId"].intValue
requestName = jsonData["requestName"].stringValue
groupName = jsonData["groupName"].stringValue
}}
import SwiftUI
import SwiftyJSON
struct NotificationView: View {
var roles = ["userNotification", "managerNotification"]
@EnvironmentObject var userToken:UserToken
@State var show = false
@State private var selectedIndex = 0
@State var userNotifications : [UserNotification] = [UserNotification]()
@State var managerNotifications : [ManagerNotification] = [ManagerNotification]()
var body: some View {
VStack {
Picker(selection: $selectedIndex, label: Text(" ")) {
ForEach(0..<roles.count) { (index) in
Text(self.roles[index])
}
}
.pickerStyle(SegmentedPickerStyle())
containedView()
Spacer()
}
.onAppear(perform: getAllNotification)
}
func containedView() -> AnyView {
switch selectedIndex {
case 0:
return AnyView(
List(userNotifications) { userNotification in
UserNotificationCellView(userNotification: userNotification)
}
)
case 1:
return AnyView(
List(managerNotifications) { managernotification in
ManagerNotificationCellView(managerNotification : managernotification)
}
.onAppear(perform: getManagerNotification)
)
default:
return AnyView(Text("22").padding(40))
}
}
func getAllNotification(){
// if (self.userNotifications.count != 0){
// self.userNotifications.removeAll()
// }
// I think the crash was in here, because when i don't use removeAll().
// It works fine, but i don't want every times i change to this view. my array will be longer and
// longer
if (self.managerNotifications.count != 0){
self.managerNotifications.removeAll()
}
NetWorkController.sharedInstance.connectApiByPost(api: "/User/email", params: ["token": "\(self.userToken.token)"])
{(jsonData) in
if let result = jsonData["msg"].string{
print("eeee: \(result)")
if(result == "you dont have any email"){
}else if(result == "success get email"){
if let searchResults = jsonData["mail"].array {
for notification in searchResults {
self.userNotifications.append(UserNotification(jsonData: notification))
}
}
}
}
}
NetWorkController.sharedInstance.connectApiByPost(api: "/Manager/email", params: ["token": "\(self.userToken.token)"])
{(jsonData) in
if let result = jsonData["msg"].string{
print("eeee: \(result)")
if(result == "you dont have any email"){
}else if(result == "success get email"){
if let searchResults = jsonData["mail"].array {
for notification in searchResults {
self.managerNotifications.append(ManagerNotification(jsonData: notification))
}
}
}
}
}
}
func getManagerNotification(){
// if (self.managerNotifications.count != 0){
// self.managerNotifications.removeAll()
// }
print(self.managerNotifications.count)
NetWorkController.sharedInstance.connectApiByPost(api: "/Manager/email", params: ["token": "\(self.userToken.token)"])
{(jsonData) in
if let result = jsonData["msg"].string{
print("eeee: \(result)")
if(result == "you dont have any email"){
}else if(result == "success get email"){
if let searchResults = jsonData["mail"].array {
for notification in searchResults {
self.managerNotifications.append(ManagerNotification(jsonData: notification))
}
}
}
}
}
}}
错误消息
仅警告一次: UITableView被告知布局其可见单元格和其他内容,而不是在视图层次结构中(表视图或其超级视图之一尚未添加到窗口)。这可能会通过强制表格视图内的视图在没有准确信息(例如表格视图边界、特征集合、布局边距、安全区域插入等)的情况下加载和执行布局而导致错误,并且还将由于额外的布局传递而导致不必要的性能开销。在UITableViewAlertForLayoutOutsideViewHierarchy处创建一个符号断点,以便在调试器中捕获此操作并查看导致此操作的原因,这样您就可以在可能的情况下完全避免此操作,或者将其推迟到将表视图添加到窗口中。原因:‘尝试删除节0,但在更新之前只有0个节’
发布于 2020-01-14 07:53:39
我认为您对@State和@ObservebableObject的角色感到困惑;它不像MVC那样,在MVC中用SwiftUI.View替换ViewController,就像您在示例中尝试做的那样。相反,视图应该是某个本地@State和/或外部@ObservedObject的函数。这更接近于MVVM,在MVVM中,您的@ObservedObject类似于ViewModel,视图将根据ObservableObject上的@Published属性的更改进行自我重建。
TLDR:将您的获取逻辑移动到一个ObservableObject,并使用@Published来允许视图订阅结果。这里有一个例子:https://github.com/joshuajhomann/TVMaze-SwiftUI-Navigation
https://stackoverflow.com/questions/59723376
复制相似问题