Post

Replies

Boosts

Views

Activity

Long Hang Times When Accessing View With Posts
For some reason, every time I'm accessing a view populated with posts and fetching posts, my app has like a 5-8 second hang time. I don't know whats causing this issue, and I have been dealing with this for 3 days. `func fetchPosts(completion: @escaping (Result<[PostWithUser], Error>) -> Void) { if !cachedPosts.isEmpty { completion(.success(self.cachedPosts)) return } postsRef.order(by: "timestamp", descending: true).getDocuments { [weak self] snapshot, error in guard let self = self else { return } if let error = error { completion(.failure(error)) return } guard let documents = snapshot?.documents else { completion(.success([])) return } let group = DispatchGroup() var postsWithUsers: [PostWithUser] = [] DispatchQueue.global(qos: .userInitiated).async { for document in documents { group.enter() let data = document.data() guard let userId = data["userId"] as? String, let parentId = data["parentId"] as? String, let groupId = data["groupId"] as? String, let text = data["text"] as? String, let isPinned = data["isPinned"] as? Bool, let imageUrl = data["imageUrl"] as? String, let videoUrl = data["videoUrl"] as? String, let timestamp = data["timestamp"] as? Timestamp, let views = data["views"] as? Int else { group.leave() continue } let post = Post(id: document.documentID, userId: userId, parentId: parentId, groupId: groupId, text: text, imageUrl: imageUrl, videoUrl: videoUrl, timestamp: timestamp.dateValue(), isPinned: isPinned, likedBy: [], views: views) self.usersRef.document(userId).getDocument { userDocument, error in defer { group.leave() } if let userDocument = userDocument, let userData = userDocument.data() { let user = User( id: userId, username: userData["username"] as? String ?? "", bio: userData["bio"] as? String ?? "", profilePictureUrl: userData["profileImageUrl"] as? String ?? "", privateProfile: userData["privateProfile"] as? Bool ?? false, privateFollowerList: userData["privateFollowerList"] as? Bool ?? false, privateFollowingList: userData["privateFollowingList"] as? Bool ?? false, privateReplies: userData["privateReplies"] as? Bool ?? false, privateLikes: userData["privateLikes"] as? Bool ?? false ) let postWithUser = PostWithUser(post: post, user: user) DispatchQueue.main.async { postsWithUsers.append(postWithUser) } } else { print("Failed to fetch user data for userId: \(userId), error: \(String(describing: error))") } } } group.notify(queue: .main) { self.cachedPosts = postsWithUsers completion(.success(postsWithUsers)) } } } }` ` func fetchPosts() { PostService.shared.fetchPosts { result in switch result { case .success(let posts): DispatchQueue.main.async { self.posts = posts } case .failure(let error): print("Failed to fetch posts: \(error)") } } } func refreshPosts() { PostService.shared.refreshPosts { result in switch result { case .success(let posts): DispatchQueue.main.async { self.posts = posts } case .failure(let error): print("Failed to fetch posts: \(error)") } } }
1
0
324
Jun ’24
no exact matches in reference to static method 'buildExpression'
This error ( no exact matches in reference to static method 'buildExpression' ) keeps occurring in my content view on the line that states .environmentObject(registrationViewModel). Here is the ContentView code: import SwiftUI struct ContentView: View { @StateObject var viewModel = ContentViewModel() @StateObject var registrationViewModel = RegistrationViewModel() var body: some View { Group { if viewModel.userSession == nil { LoginView() .environmentObject(registrationViewModel) } else if let currentUser = viewModel.currentUser { MainTabView(user: currentUser) } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } Here is the RegistrationViewModel code: import Foundation class RegistrationViewModel: ObservableObject { @Published var username = "" @Published var email = "" @Published var password = "" func createUser() async throws { try await AuthService.shared.createUser(email: email, password: password, username: username) } } Here is some more info about the error: Candidate requires that 'some View' conform to 'CustomizableToolbarContent' (requirement specified as 'Content' : 'CustomizableToolbarContent') Candidate requires that 'some View' conform to 'CustomizableToolbarContent' (requirement specified as 'Content' : 'CustomizableToolbarContent') Candidate requires that 'some View' conform to 'TableRowContent' (requirement specified as 'Content' : 'TableRowContent') Candidate requires that 'some View' conform to 'Commands' (requirement specified as 'Content' : 'Commands') I have been trying to figure out this error for 2 days! Please help! Thanks
2
0
2.7k
Jul ’23