Wednesday, October 28, 2026
HomeiOS Developmentios - How can I move a variable into this operate (on...

ios – How can I move a variable into this operate (on this occasion)?

[ad_1]

I’m making an attempt to show any put up (firebase doc) that accommodates a selected “postCreator” worth. I’ve the question arrange and dealing – the issue is making an attempt to get them to really present.

Only for clarification, if postCreator is “zbakaaa” then all paperwork with “zbakaaa” as its postCreator worth in Firebase ought to present on the app.

Once I run code like this, the app works correctly – however just for that particular person ID. I’m making an attempt to get the code to run in order that I can one way or the other set “userId” to put up.postCreator, so when a person presses on the button to open this sheet, the put up.postCreator is handed into the UserViewModel (class that following code is in) after which set to userId.

init() {
        getUserPosts()
    }
    
    func getUserPosts() {
        let userId = "rgOOOwD2cNdayJodbMgGGy9l9pB3"
        ref.assortment("Posts").whereField("postCreator", isEqualTo: userId).addSnapshotListener { (snap, err) in...

I’ve tried one thing like this (which I’ve working in an unrelated utility), however this simply throws errors.

var put up: PostModel? {
        didSet{
            if let put up = self.put up {
                getUserPosts(userId: put up.postCreator)
            }
        }
    }

Right here is my program code for reference:

PostModel

struct PostModel : Identifiable {
    
    var id: String
    var title: String
    var pic: String
    var likes: Int64
    var time: Date
    var likedBy: [String]
    var person: UserModel
    var postCreator: String
    
}

Button pressed to open this sheet (the place postCreator ought to be put in?)

var put up : PostModel
@State personal var showingUserProfile = false
...

Button(motion: {
                    showingUserProfile = true
                }){
                    WebImage(url: URL(string: put up.person.pic)!)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .body(width: 50, top: 50)
                        .clipShape(Circle())
                }   
                .transition(.transfer(edge: .main))
                .sheet(isPresented: self.$showingUserProfile)
                {
                    UserView(put up:put up)
                }

UserViewModel Class

class UserViewModel : ObservableObject{
    
    @Revealed var posts : [PostModel] = []
    @Revealed var noPosts = false
    @Revealed var newPost = false
    @Revealed var updateId = ""
    
    let ref = Firestore.firestore()
    let uid = Auth.auth().currentUser!.uid
    
    init() {
        getUserPosts()
    }
    
    func getUserPosts() {
        let userId = "rgOOOwD2cNdayJodbMgGGy9l9pB3"
        ref.assortment("Posts").whereField("postCreator", isEqualTo: userId).addSnapshotListener { (snap, err) in
            guard let docs = snap else{
                self.noPosts = true
                return
            }

...

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments