Ios 阵列更改时SwiftUI列表不更新

Ios 阵列更改时SwiftUI列表不更新,ios,swift,firebase,swiftui,swift4,Ios,Swift,Firebase,Swiftui,Swift4,当每个列表元素出现时,它会检查它是否是数组中的最后一个元素。如果是,它将从Firestore获取更多信息并更新fetchedPosts。但是,当数组更新时,列表不会更新,因此不会显示新元素 这是ObserveObject,它发布数组 private func elementOnAppear(_ post: Post) { /* onAppear on the view is called when a view appears on screen. elementOnAppea

当每个列表元素出现时,它会检查它是否是数组中的最后一个元素。如果是,它将从Firestore获取更多信息并更新
fetchedPosts
。但是,当数组更新时,列表不会更新,因此不会显示新元素

这是ObserveObject,它发布数组

private func elementOnAppear(_ post: Post) {
    /* onAppear on the view is called when a view appears on screen.
     elementOnAppear asks the view model if the element is the last one.
     If so, we ask the view model to fetch new data. */
    
    if self.postsData.isLastPostInList(post: post) {
        self.postsData.fetchPosts(userInfo: self.userInfo)
    }
}
有什么解决办法吗?

有几件事

class SocialObservable: ObservableObject{
    let db = Firestore.firestore()
    
    let objectWillChange = ObservableObjectPublisher()
    
    @Published var fetchedPosts = [Post]()
    
    @Published var lastSnap : DocumentSnapshot?
    @Published var reachedEnd = false
    
    var currentListener: ListenerRegistration?
    
    var newFetch = false {
        willSet{
            objectWillChange.send()
        }
    }
    
    init(userInfo: UserData){
        print(userInfo.uid)
        fetchPosts(userInfo: userInfo)
    }
    
    func fetchPosts(userInfo: UserData){
        var first: Query?
        
        if lastSnap == nil || newFetch {
            //not last snapshot, or just updated feed
            
            if newFetch{
                newFetch.toggle()
                fetchedPosts = [] // clean up if new fetch
            }
            
            first = db.collection("posts")
                .whereField("availability", arrayContains: userInfo.uid)
                .order(by: "date", descending: true)
                .limit(to: 1)
        }
        else {
            first = db.collection("posts")
                .whereField("availability", arrayContains: userInfo.uid)
                .order(by: "date", descending: true)
                .start(afterDocument: lastSnap!)
                .limit(to: 1)
        }

        first?.getDocuments(completion: { (snapshot, error) in
            guard let snapshot = snapshot else {
                print("Error: \(error.debugDescription)")
                return
            }
            let doc = snapshot.documents.map({postFromDB(obj: $0.data(), id: $0.documentID)})
            
            doc.map({print($0.postID)})
            // append to fetched posts
            self.fetchedPosts = self.fetchedPosts + doc
            print(self.fetchedPosts.count)
            
            //prepare for the next fetch
            guard let lastSnapshot = snapshot.documents.last else {
                // the collection is empty. no data fetched
                self.reachedEnd = true
                return
            }
            // save last snapshot
            self.lastSnap = lastSnapshot
        })
        
        
    }
    
    
    
    func isLastPostInList(post: Post) -> Bool {
        return post.postID == fetchedPosts.last?.postID
    }
    
    
}

postsData
定义为:
@ObservedObject让postsData:socialobserveble
?-它必须是一个
@ObservedObject
,视图才能因更新而刷新。顺便说一句,如果您要更改
@Published
属性,您不需要同时使用
objectWillChange.send
Hmm我将其更改为
@ObservedObject var postsData=SocialObservable(userInfo:UserData())
,但问题仍然是一样的。。。对于
objectWillChange.send
,请确保我可以将其更改为@published属性-我只希望视图能够切换newFetch。
class SocialObservable: ObservableObject{
    let db = Firestore.firestore()
    
    let objectWillChange = ObservableObjectPublisher()
    
    @Published var fetchedPosts = [Post]()
    
    @Published var lastSnap : DocumentSnapshot?
    @Published var reachedEnd = false
    
    var currentListener: ListenerRegistration?
    
    var newFetch = false {
        willSet{
            objectWillChange.send()
        }
    }
    
    init(userInfo: UserData){
        print(userInfo.uid)
        fetchPosts(userInfo: userInfo)
    }
    
    func fetchPosts(userInfo: UserData){
        var first: Query?
        
        if lastSnap == nil || newFetch {
            //not last snapshot, or just updated feed
            
            if newFetch{
                newFetch.toggle()
                fetchedPosts = [] // clean up if new fetch
            }
            
            first = db.collection("posts")
                .whereField("availability", arrayContains: userInfo.uid)
                .order(by: "date", descending: true)
                .limit(to: 1)
        }
        else {
            first = db.collection("posts")
                .whereField("availability", arrayContains: userInfo.uid)
                .order(by: "date", descending: true)
                .start(afterDocument: lastSnap!)
                .limit(to: 1)
        }

        first?.getDocuments(completion: { (snapshot, error) in
            guard let snapshot = snapshot else {
                print("Error: \(error.debugDescription)")
                return
            }
            let doc = snapshot.documents.map({postFromDB(obj: $0.data(), id: $0.documentID)})
            
            doc.map({print($0.postID)})
            // append to fetched posts
            self.fetchedPosts = self.fetchedPosts + doc
            print(self.fetchedPosts.count)
            
            //prepare for the next fetch
            guard let lastSnapshot = snapshot.documents.last else {
                // the collection is empty. no data fetched
                self.reachedEnd = true
                return
            }
            // save last snapshot
            self.lastSnap = lastSnapshot
        })
        
        
    }
    
    
    
    func isLastPostInList(post: Post) -> Bool {
        return post.postID == fetchedPosts.last?.postID
    }
    
    
}
class SocialObservable: ObservableObject{
    let db = Firestore.firestore()
    
    // let objectWillChange = ObservableObjectPublisher()     // << remove this
    
    // ...
    
    var newFetch = false {
        willSet{
            self.objectWillChange.send()    // ObservableObject has default
        }
    }

    // ...
    doc.map({print($0.postID)})
    // append to fetched posts
    DispatchQueue.main.async {
       self.fetchedPosts = self.fetchedPosts + doc
    }
    print(self.fetchedPosts.count)