0

I'm new to coding and have been trying to go through instagram tutorials to understand some concepts. Since updating to Xcode 12, my Firebase has seemed to not work anymore and is not showing on the home feed. I placed a rectangle in to see if it was the if !homeViewModel.isLoading was the cause it appears to be so.

enter image description here

Here is my current code:

import SwiftUI
import URLImage
import Firebase

struct HomeView: View {
    
    @ObservedObject var homeViewModel = HomeViewModel()
    
    var body: some View {
        NavigationView {
            
            ScrollView(.vertical, showsIndicators: false) {
                
                Story()
                Rectangle().frame(width: 200, height: 200).foregroundColor(.red)
                if !homeViewModel.isLoading {
                    ForEach(self.homeViewModel.posts, id: \.postId) { post in
                        VStack(alignment: .center) {
                            
                            HeaderCell(post: post)
                            FooterCell(post: post)
                            
                            }.background(Color.white).cornerRadius(10)
                            .padding(.leading, 10).padding(.trailing, 10)
                    }
                    
                }
                }.background(Color.gray)

HomeViewModel:

import Foundation
import SwiftUI
import Firebase

class HomeViewModel: ObservableObject {
    @Published var posts: [Post] = []
    @Published var isLoading = false
    
    var listener: ListenerRegistration!
    
//    init() {
//        loadTimeline()
//    }
    
    func loadTimeline() {
        self.posts = []
        isLoading = true
        
        Api.Post.loadTimeline(onSuccess: { (posts) in
            self.isLoading = false

            if self.posts.isEmpty {
                self.posts = posts
            }
        }, newPost: { (post) in
            if !self.posts.isEmpty {
                self.posts.insert(post, at: 0)
            }
        }) { (listener) in
            self.listener = listener
        }

    }
}

LoadTimeline func:

func loadTimeline(onSuccess: @escaping(_ posts: [Post]) -> Void, newPost: @escaping(Post) -> Void, listener: @escaping(_ listenerHandle: ListenerRegistration) -> Void) {
        guard let userId = Auth.auth().currentUser?.uid else {
            return
        }
        let listenerFirestore = Ref.FIRESTORE_TIMELINE_DOCUMENT_USERID(userId: userId).collection("timelinePosts").order(by: "date", descending: true).addSnapshotListener({ (querySnapshot, error) in
            guard let snapshot = querySnapshot else {
                return
            }
            var posts = [Post]()
            snapshot.documentChanges.forEach { (documentChange) in
                switch documentChange.type {
                case .added:
                    print("type: added")
                    let dict = documentChange.document.data()
                    guard let decoderPost = try? Post.init(fromDictionary: dict) else {return}
                    newPost(decoderPost)
                    posts.append(decoderPost)
                case .modified:
                    print("type: modified")
                case .removed:
                    print("type: removed")
                }
            }
            onSuccess(posts)
        })
        
        listener(listenerFirestore)
        
    }

For some reason it seems as though the function isn't being triggered and timeline isn't loading. unsure why though... Prior to the update it was working fine?

Any help would be much appreciated!

  • Does this answer your question? [App Delegate/Scene Delegate and Firebase issues after updating from Xcode 11 to 12](https://stackoverflow.com/questions/64202780/app-delegate-scene-delegate-and-firebase-issues-after-updating-from-xcode-11-to) – Peter Friese Oct 08 '20 at 08:10
  • Isn't this a duplicate of your prior question? If not, can you update it so it's more clear? It seems to be the same problem - your UI isn't updating with data from Firebase. – Jay Oct 08 '20 at 19:45

0 Answers0