Home > Software engineering >  Even if only other users click on the profile, only my profile is visible
Even if only other users click on the profile, only my profile is visible

Time:10-20

Even if you click on someone else's profile, only users who are currently logged in are displayed. A few similar questions have been asked, but I ask because the language and environment are different.

To solve the problem, the displayed ID is printed, but the printed ID is displayed as a different user. So maybe there's a problem with the profile controller, right? Or is there a problem somewhere else?

FeedCellDelegate:

protocol FeedCellDelegate: class {
    func cell(_ cell: FeedCell, wantsToShowCommentsFor post: Post)
    func cell(_ cell: FeedCell, didLike post: Post)
    func cell(_ cell: FeedCell, wantsToShowProfileFor uid: String)
}

ProfileController:

extension ProfileController {
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return posts.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ProfileCell
        cell.viewModel = PostViewModel(post: posts[indexPath.row])
        return cell
    }
    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! ProfileHeader
        header.delegate = self
       
            header.viewModel = ProfileHeaderViewModel(user: user)
            return header
    }
}

Feed controller:

 func cell(_ cell: FeedCell, wantsToShowProfileFor uid: String) {
        UserService.fetchUser(withUid: uid) { user in
            print("id check \(uid)")
            let controller = ProfileController(user: user)
            self.navigationController?.pushViewController(controller, animated: true)
        }

UserService(firebase)

 static func fetchUser(withUid uid: String, completion: @escaping(User) -> Void) {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        Collection_Users.document(uid).getDocument { snapshot, error in
            guard let dictionary = snapshot?.data() else { return }
            
            let user = User(dictionary: dictionary)
            completion(user)
        }
    }

FeedCell:

    @objc func showUserProfile() {
        guard let viewModel = viewModel else { return }
        print("check UID : \(viewModel.post.ownerUid)")
        delegate?.cell(self, wantsToShowProfileFor: viewModel.post.ownerUid)
        
    }

CodePudding user response:

In your fetchUser function you always do:

guard let uid = Auth.auth().currentUser?.uid else { return }
Collection_Users.document(uid).getDocument { snapshot, error in

So you always get the user document for Auth.auth().currentUser?.uid, regardless of what else happened in the app.

If you wan to load the document for a different user, you'll have to pass the UID of the user that was clicked on to fetchUser and use that UID in the call to document.

  • Related