我正在处理我的应用程序的用户个人资料页面。我有一个默认设置为 false 的全局布尔变量(updateProfile)。当用户对他们的个人资料信息进行任何更改时,例如更改/删除他们的个人资料图片,数据库会更新,图像会下载并保存在文档目录中。这是下载后保存该图像的代码:
struct Downloads {
// Create a static variable to start the download after tapping on the done button in the editUserProfile page
static var updateProfile: Bool = false
static func downloadUserProfilePic() {
Database.database().reference().child("Users").child(userID!).child("Profile Picture URL").observeSingleEvent(of: .value) { (snapshot) in
guard let profilePictureString = snapshot.value as? String else { return }
guard let profilePicURL = URL(string: profilePictureString) else { return }
let session = URLSession(configuration: .default)
let downloadPicTask = session.dataTask(with: profilePicURL) {
(data, response, error) in
if let e = error {
print("error downloading with error: \(e)")
} else {
if let res = response as? HTTPURLResponse {
Downloads.imageCached = true // The image has been downloaded
print("Downloaded with \(res.statusCode)")
if let imageData = data {
let image = UIImage(data: imageData)
// Now save the image in the documents directory
// Get the url of the documents directory
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// Name your image with the user ID to make it unique
let imageName = userID! + "profilePic.jpg"
// Create the destination file URL to save the image
let imageURL = documentsDirectory.appendingPathComponent(imageName)
print(imageURL)
let data = image?.jpegData(compressionQuality: 0.5)
do {
// Save the downloaded image to the documents directory
// Write the image data to disk
try data!.write(to: imageURL)
print("Image Saved")
updateProfile = true
} catch {
print("Error saving file \(error)")
}
} else {
print("Couldnt get image")
}
} else {
print("Couldnt't get response")
}
}
}
downloadPicTask.resume()
}
}
SomeOtherViewController
// When the user taps on the 'done' button
@objc func doneButtonTapped() {
uploadUserSelectedPicture()
}
func uploadUserSelectedPicture() {
// Download the profile picture and save it
Downloads.downloadUserProfilePic()
if Downloads.updateProfile == true {
// Go to the user profile page
let userProfilePage = UserProfilePage()
self.present(userProfilePage, animated: true)
}
}
如您所见,只要将图像保存到文档目录并且 updateProfile 全局变量更改为 true,我就会打印“图像已保存”。而在 SomeOtherViewController 上,仅当 updateProfile 变量为真时(这意味着图像应保存到文档目录)才会显示页面(当点击完成按钮时)。
但唯一的问题是,该变量在图像保存之前设置为 true,我怎么知道呢?我知道这一点是因为该页面是在执行 print 语句之前呈现的print("Image Saved")
。为什么会这样?有什么办法可以摆脱这个问题?