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.