Posts

Post not yet marked as solved
1 Replies
558 Views
Hello my Xcode app fetches the current user from my database and stores their data in a User struct. And this data is needed for other portions of the app. I use an EnvirnmentObject to translate this data to other views. But the problem Im running in is: if I modify a variable in the struct of the current user in one view then navigate to another view this change is not observed in the new view. I don't think you can make @Published vars static, but what's another approach so that if I modify the variable "followers" for example in one view then all the others views will also have this change. I want to do this so I don't have to fetch the current user from the database every time I make changes to the current user. getting the current user: import SwiftUI import Firebase class AuthViewModel: ObservableObject{ private let service = UserService() @Published var currentUser: User? init(){ self.fetchUser() } func fetchUser(){ service.fetchUser() { user in self.currentUser = user } } } view 1 import SwiftUI struct viewOne: View { @EnvironmentObject var authViewModel: AuthViewModel var body: some View { VStack(alignment: .center, spacing: 2){ AuthViewModel.currentUser.followers = 25 } } } view 2 - change in followers var is not seen import SwiftUI struct viewOne: View { @EnvironmentObject var authViewModel: AuthViewModel var body: some View { VStack(alignment: .center, spacing: 2){ Text("\(AuthViewModel.currentUser.followers)") // not 25 } } }
Posted Last updated
.
Post not yet marked as solved
0 Replies
244 Views
Hello, I have an Xcode app with a few thousand users and I use firestore to store my data. Every time a user registers firestore creates a random uid for them. Right now I have all my users in one big collection called "users." I was thinking about splitting up this collection into sub collections for each letter in the alphabet and then putting each user, based on the first letter of their name, in the corresponding sub collection. But the only problem with this is when I want to fetch the current user. When a user signs into the app I only have their uid from the firestore authentication. So if I want to fetch the current user I wouldn't really have any pointer to which sub collection to look into. I thought of maybe putting a pointer letter at the beginning of the UID but I don't think firestore allows me to change Uids. Does anyone know a better approach to this or how I can make this work. Id appreciate the help.
Posted Last updated
.
Post not yet marked as solved
1 Replies
974 Views
Hello, I have been playing around with preference keys and geometry readers but I can't figure out a way to actually get the real size of a scrollview or vstack inside a scroll view. I have 2 different approaches and they both semi work, the problem with them is that when I scroll through the scroll view the size fluctuates. For example if the size of the vstack in the scroll view is 5000, then the size I get from my methods will fluctuate between 4200-5900 as I scroll through the view. Im not sure why this is, can anyone show me a solid way to get the height of a vstack inside a scrollview. method 1                     ScrollView {                         LazyVStack {                             ForEach(viewModel.new.) { i in someView(i)                             }                         }                         .background(GeometryReader { proxy in                             Color.clear                                 .onChange(of: offset) { _ in                                     print("Height of VStack:", proxy.size.height)                                 }                         })                     } method 2 ScrollView {                         ChildSizeReader(size: $scrollViewSize) {                             LazyVStack {                                 ForEach(viewModel.new) { i in someView(i)                                     }                                 }                             }                         }                     } struct ChildSizeReader<Content: View>: View {   @Binding var size: CGSize   let content: () -> Content   var body: some View {     ZStack {       content().background(         GeometryReader { proxy in           Color.clear.preference(             key: SizePreferenceKey.self,             value: proxy.size           }         }       }     }     .onPreferenceChange(SizePreferenceKey.self) { preferences in       self.size = preferences     }   } } struct SizePreferenceKey: PreferenceKey {   typealias Value = CGSize   static var defaultValue: Value = .zero   static func reduce(value _: inout Value, nextValue: () -> Value) {     _ = nextValue()   } }
Posted Last updated
.
Post marked as solved
1 Replies
452 Views
so I have two functions the first gets an array of structs and the second func gets another struct object and appends it to the first array. In my view I have a scroll view that displays all these elements in the array. When I run the code with the getAd function commented out (does not execute at all) the view displays the correct array of elements in the scroll view. but when I uncomment the getAd function the view does not display any elements at all. But I added a button to print the viewModel.new array, and when I click it, the correct array is printed out WITH the final element appended. Since the app works fine when the second function is commented out, it leads me to believe that the second func is causing the problem, but when I print the array with the button, the result looks just fine how I want it to look. Im not sure what the problem is, maybe the asynchronous nature of the functions. view import SwiftUI struct FeedView: View { @StateObject var viewModel = FeedViewModel() var body: some View { mainInterFaceView } } extension FeedView{ var mainInterFaceView: some View{ VStack(){ ZStack(){ ScrollView { LazyVStack { ForEach(viewModel.new) { tweet in TweetRowView(tweet: tweet, hasDivider: true) } } } } } } } } viewModel: class FeedViewModel: ObservableObject{ @Published var new = [Tweet]() var ads = [Tweet]() let service = TweetService() let userService = UserService() init(){ fetchNew() } func fetchNew(){ service.fetchNew { tweets in self.new = tweets for i in 0 ..< tweets.count { let uid = tweets[i].uid self.userService.fetchUser(withUid: uid) { user in self.new[i].user = user } } self.getAd() //here } } func getAd() { service.fetchAd { ads in self.ads = ads for i in 0 ..< ads.count { let uid = ads[i].uid self.userService.fetchUser(withUid: uid) { user in self.ads[i].user = user } } self.new.append(self.ads[0]) } }
Posted Last updated
.
Post not yet marked as solved
0 Replies
374 Views
Hello, my Xcode app has worked just fine for the last few weeks, I have not changed anything and when I run it now, the tab bar disappears when the associated tab is selected, also stuff like the cursor, navigationbarbackbutton are all invisible or white. How can I fix this, not sure if it's an Xcode bug. tab view:     init() {         let tabBarAppearance = UITabBarAppearance()         tabBarAppearance.configureWithOpaqueBackground()         UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance     } content view:         TabView(selection: $selectedTab){             NavigationView(){                 FeedView()             }             .tabItem {                 Image(systemName: "h.circle")             }.tag(1)             NavigationView(){                 JobsView()             }             .tabItem {                 Image(systemName: "j.circle")                     .tint(.gray)             }.tag(2)         }
Posted Last updated
.