首页 > 解决方案 > ViewDidAppear 不从 Firebase 更新我的图片,但更新文本信息

问题描述

我对 IOS 编程还是很陌生,在使用 Firebase 数据库和存储时遇到了问题。这是图像:

编辑文件之前

这是配置文件在编辑之前的样子。如果用户点击编辑按钮,我们将进入编辑模式。

编辑期间

在编辑过程中,我们更改了它的名称、描述、人物图标和横幅图片,当我们完成它时,它会回到这个配置文件并查看更新的文件。但是,编辑后,它返回的是这样的:

编辑后

名称和描述确实改变了,但头像没有改变。事实上,如果我再次编辑配置文件,它将更新到我更改的最后一张图片。如果我退出并返回个人资料页面,它将显示正确的图片,这是最后更新的图片。这是我在配置文件视图控制器中的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    print("The center of phone is: \(view.center.x) + \(view.center.y)")
    print("The center of pic is: \(PersonIcon.center.x) + \(Banner.center.y)")
    UserDataManager.getUserInfo(uid: user!.uid) { (userData) in
        self.ProfileName.text = userData["UserName"]!
        if (userData["UserBio"] != "NULL") {
            self.ProfileBio.text = userData["UserBio"]!
        }
        if (userData["UserIconURL"] != "NULL") {
            let url = URL(string: userData["UserIconURL"]!)
            let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, _, error) in
                guard let data = data, error == nil else {
                    print("Error trying to download user icon")
                    return
                }
                
                DispatchQueue.main.async {
                    let image = UIImage(data: data)
                    self.PersonIcon.image = image
                    print("Should be Updated?")
                }
            })
            task.resume()
        }
        if (userData["UserBannerURL"] != "NULL") {
            let url = URL(string: userData["UserBannerURL"]!)
            let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, _, error) in
                guard let data = data, error == nil else {
                    print("Error trying to download user Banner")
                    return
                }
                
                DispatchQueue.main.async {
                    let image = UIImage(data: data)
                    self.Banner.image = image
                    print("Should be Updated?")
                }
            })
            task.resume()
        }
    }
    makeRounded(ProfileImage: PersonIcon)

    // Do any additional setup after loading the view.
}

并且函数ViewDidAppear是完全相同的代码(除了super.viewDidAppear(animated))。我觉得这与线程有关,因为我确实将下载线程与主线程异步以确保配置文件得到更新,但我对 IOS 中的线程仍然很陌生,无法判断这是否是这里的问题. 请帮忙,谢谢!

编辑用户信息的代码:

@IBAction func onSubmit(_ sender: Any) {
    // Todo: Submit the data to server and update user info
    let DataManager = FirebaseDataAccessManager()
    var url_id = "Nothing"
    
    // To make sure we get uid before we upload, all the code regarding upload must be put inside this closure
    DataManager.getUserInfo(uid: user!.uid) { (userData) in
        url_id = userData["URL_ID"]!
        DataManager.updateUserIcon(URL_ID: url_id, image: self.PersonIcon.image!) { (success, urlString) in
            if (success) {
                print("PersonIcon Upload Success!")
                print("URL stored into the data is: \(urlString)")
                DataManager.updateUserIconURL(uid: user!.uid, url: urlString)
            }
        }
        DataManager.updateUserBanner(URL_ID: url_id, image: self.Banner.image!) { (success, urlString) in
            if (success) {
                print("PersonBanner Upload Success!")
                print("URL stored into the data is: \(urlString)")
            }
            DataManager.updateUserBannerURL(uid: user!.uid, url: urlString)
        }
    }
    
    DataManager.updateUserSetting(uid: user!.uid, UserName: self.PersonName.text as! String, UserBio: self.PersonBio.text as! String, UserKeyword: self.PersonKeyword.text as! String) { (success) in
        if (success) {
            self.navigationController?.popViewController(animated: true)
        }
        else {
            print("We're not going anywhere cuz update failed!")
        }
    }
    

    self.navigationController?.popViewController(animated: true)
}

让我知道您是否还需要在DataManager. 它是一个包含 Firebase 数据库 API 调用的辅助类。

代码viewDidAppear()

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        UserDataManager.getUserInfo(uid: user!.uid) { (userData) in
            self.ProfileName.text = userData["UserName"]!
            if (userData["UserBio"] != "NULL") {
                self.ProfileBio.text = userData["UserBio"]!
            }
            if (userData["UserIconURL"] != "NULL") {
                let url = URL(string: userData["UserIconURL"]!)
                let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, _, error) in
                    guard let data = data, error == nil else {
                        print("Error trying to download user icon")
                        return
                    }
                    
                    DispatchQueue.main.async {
                        let image = UIImage(data: data)
                        self.PersonIcon.image = image
                    }
                })
                task.resume()
            }
            if (userData["UserBannerURL"] != "NULL") {
                let url = URL(string: userData["UserBannerURL"]!)
                let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, _, error) in
                    guard let data = data, error == nil else {
                        print("Error trying to download user Banner")
                        return
                    }
                    
                    DispatchQueue.main.async {
                        let image = UIImage(data: data)
                        self.Banner.image = image
                    }
                })
                task.resume()
            }
        }

        makeRounded(ProfileImage: PersonIcon)
    }

更新:感谢@Pratik Prajapati,问题是我在图片更新之前过早地刷新了页面,所以我已经将我的代码重新写入其中,并且它有效!

@IBAction func onSubmit(_ sender: Any) {
        // Todo: Submit the data to server and update user info
        let DataManager = FirebaseDataAccessManager()
        var url_id = "Nothing"
        
        DataManager.updateUserSetting(uid: user!.uid, UserName: self.PersonName.text!, UserBio: self.PersonBio.text as! String, UserKeyword: self.PersonKeyword.text as! String) { (success) in
            if (success) {
                DataManager.getUserInfo(uid: user!.uid) { (userData) in
                    url_id = userData["URL_ID"]!
                    DataManager.updateUserIcon(URL_ID: url_id, image: self.PersonIcon.image!) { (success, urlString) in
                        if (success) {
                            print("PersonIcon Upload Success!")
                            print("URL stored into the data is: \(urlString)")
                            DataManager.updateUserIconURL(uid: user!.uid, url: urlString)
                            
                            DataManager.updateUserBanner(URL_ID: url_id, image: self.Banner.image!) { (success, urlString) in
                                if (success) {
                                    print("PersonBanner Upload Success!")
                                    print("URL stored into the data is: \(urlString)")
                                    self.navigationController?.popViewController(animated: true)
                                }
                                DataManager.updateUserBannerURL(uid: user!.uid, url: urlString)
                            }
                        }
                    }
                }
            }
            else {
                print("We're not going anywhere cuz update failed!")
            }
        }
        // To make sure we get uid before we upload, all the code regarding upload must be put inside this closure
    }

但是,我确实认为这不是正确的方法。我实际上已经将 3 个完成块链接在一起,上传实际上需要一秒钟才能从编辑切换回配置文件。我肯定做错了什么。关于如何改进此代码的任何建议?如果我有 10 个信息而不是 3 个要更新怎么办?我不能将 10 个完成块链接在一起!

无论如何,感谢提供信息的家伙。我确实学到了很多东西!

标签: iosswiftfirebaseios-multithreading

解决方案


感谢@Pratik Prajapati,问题是我在图片更新之前过早地刷新了页面,所以我将我的代码重新写入其中,并且它有效!

@IBAction func onSubmit(_ sender: Any) {
    // Todo: Submit the data to server and update user info
    let DataManager = FirebaseDataAccessManager()
    var url_id = "Nothing"
    
    DataManager.updateUserSetting(uid: user!.uid, UserName: self.PersonName.text!, UserBio: self.PersonBio.text as! String, UserKeyword: self.PersonKeyword.text as! String) { (success) in
        if (success) {
            DataManager.getUserInfo(uid: user!.uid) { (userData) in
                url_id = userData["URL_ID"]!
                DataManager.updateUserIcon(URL_ID: url_id, image: self.PersonIcon.image!) { (success, urlString) in
                    if (success) {
                        print("PersonIcon Upload Success!")
                        print("URL stored into the data is: \(urlString)")
                        DataManager.updateUserIconURL(uid: user!.uid, url: urlString)
                        
                        DataManager.updateUserBanner(URL_ID: url_id, image: self.Banner.image!) { (success, urlString) in
                            if (success) {
                                print("PersonBanner Upload Success!")
                                print("URL stored into the data is: \(urlString)")
                                self.navigationController?.popViewController(animated: true)
                            }
                            DataManager.updateUserBannerURL(uid: user!.uid, url: urlString)
                        }
                    }
                }
            }
        }
        else {
            print("We're not going anywhere cuz update failed!")
        }
    }
    // To make sure we get uid before we upload, all the code regarding upload must be put inside this closure
}

显然,这可以使用 dispatchgroup 以更好的格式编写,但现在这对我有用。


推荐阅读