-3

I make a scroll system like TikTok and for that I need the number of elements in my Firebase.

class countPost: ObservableObject {
    @Published var post = [Post]()
    @Published var total: Int = 0

    init() {
        Task {
            try await fetchPost()
        }
    }

    @MainActor
    func fetchPost() async throws{
        let snapshot = try await Firestore.firestore().collection("post").getDocuments()
        self.post = try snapshot.documents.compactMap({ try $0.data(as: Post.self) })

        total = post.count
    }
}

Here is the total variable:

struct PlayerScrollView: UIViewRepresentable{
    @StateObject var viewModel = FeedViewModel()
    func makeUIView(context: Context) -> UIScrollView {
        let view = UIScrollView()
        let childView = UIHostingController(rootView: FeedViewContent())
        
        childView.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height) * CGFloat(countPost().total))
        
        view.contentSize = CGSize(width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height) * CGFloat(countPost().total))
        
        view.addSubview(childView.view)
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        
        view.contentInsetAdjustmentBehavior = .never
        view.isPagingEnabled = true
        
        return view
    } 

I want to use total here but total=0 while in my count view when I do print it is 3.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • 1
    While I’ve rendered your four images included in your question, but (a) it would be more helpful to post as code rather than images; and (b) more clearly tell us what the difference is between them, because it’s not at all obvious. – Rob Sep 01 '23 at 01:01
  • There’s simultaneously too much unrelated cruft here as well as not enough to manifest/diagnose the problem. We need a simple [MRE](https://stackoverflow.com/help/minimal-reproducible-example). – Rob Sep 01 '23 at 01:05

0 Answers0