【问题标题】:Even if only other users click on the profile, only my profile is visible即使只有其他用户点击个人资料,也只有我的个人资料可见
【发布时间】:2021-12-06 07:48:54
【问题描述】:

即使您点击其他人的个人资料,也只会显示当前登录的用户。有人问过一些类似的问题,但 我问是因为语言和环境不同。

为解决问题,打印显示的 ID,但打印的 ID 显示为不同的用户。所以也许配置文件控制器有问题,对吧?还是其他地方有问题?

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)
}

配置文件控制器:

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
    }
}

饲料控制器:

 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)
        
    }

【问题讨论】:

    标签: ios swift google-cloud-firestore firebase-authentication


    【解决方案1】:

    在您的 fetchUser 函数中,您总是这样做:

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

    因此,无论应用程序中发生了什么其他情况,您总是会获得Auth.auth().currentUser?.uid 的用户文档。

    如果您想为其他用户加载文档,您必须将被点击的用户的 UID 传递给 fetchUser 并在对 @ 的调用中使用 那个 UID 987654325@.

    【讨论】:

    • 非常感谢。你是一个善良的人。我会在自己研究 fetchuser 方面时更正它。
    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    相关资源
    最近更新 更多