我已经为此苦苦挣扎了 2 个小时。我正在使用 Firebase 数据库和存储在 Swift 中构建一个应用程序。
目标是更新用户资料。用户有 2 张图像 - 个人资料和标题。现在,我必须首先检查他们是否从照片库中选择了图像,如果没有 - 只需从数据库中获取旧 URL,然后将其与其余更新信息一起提交回数据库。如果是新选择的图像,将图像上传到存储,使用 downloadURL
取回 URL,将其分配给 var storageHeaderDownloadedURL
和/或 var storageProfileDownloadedURL
并将字符串值与其余用户数据一起提交到 Firebase 数据库。
问题是,在我取回下载的 URL 之前,它显然分配了一个空 String
的值(我已经这样声明了它们)。如果用户不更新图像,但 UITextFields
的其余部分一切正常,则旧 URL 将提交到 Firebase 数据库。
我的问题是如何从存储中执行下载的 URL 方法,然后将其直接分配给 var storageHeaderDownloadedURL
和 var storageProfileDownloadedURL
?
func updateUserProfile ()
{
if let userID = FIRAuth.auth()?.currentUser?.uid
{
// Note: Storage references to profile images & profile headers folder
let storageUserProfileID = Storage.storage.profile_images.child(userID)
let storageUserHeaderID = Storage.storage.profile_headers.child(userID)
guard let imageProfile = profileImage.image else { return }
guard let headerImage = headerImage.image else { return }
if let newProfileImage = UIImagePNGRepresentation(imageProfile), let newHeaderImage = UIImagePNGRepresentation(headerImage)
{
storageUserProfileID.put(newProfileImage, metadata: nil, completion: { (metadata, error) in
if error != nil
{
showAlert(title: "Oops!", msg: (error?.localizedDescription)!, actionButton: "OK", viewController: self)
return
}
// Get the URL from the storage
storageUserProfileID.downloadURL(completion: { (url, error) in
if error != nil
{
showAlert(title: "Oops!!!", msg: (error?.localizedDescription)!, actionButton: "OK", viewController: nil)
return
}
else
{
if let profileImgDownloadedURL = url?.absoluteString
{
self.storageProfileDownloadedURL = profileImgDownloadedURL
print(self.storageProfileDownloadedURL)
self.selectedProfileImage = .True
}
}
})
})
storageUserHeaderID.put(newHeaderImage, metadata: nil, completion: { (metadata, error) in
if error != nil
{
showAlert(title: "Oops!", msg: (error?.localizedDescription)!, actionButton: "OK", viewController: self)
return
}
// Get the URL from the storage
storageUserHeaderID.downloadURL(completion: { (url, error) in
if error != nil
{
showAlert(title: "Oops!!!", msg: (error?.localizedDescription)!, actionButton: "OK", viewController: self)
return
}
else
{
if let headerImgDownloadedURL = url?.absoluteString
{
self.storageHeaderDownloadedURL = headerImgDownloadedURL
print(self.storageHeaderDownloadedURL)
self.selectedHeaderImage = .True
}
}
})
})
//Note: Update the info for that user in Database
print(self.storageHeaderDownloadedURL)
print(self.storageProfileDownloadedURL)
var finalHeaderImageURL = String()
switch self.selectedHeaderImage {
case .True:
finalHeaderImageURL = self.storageHeaderDownloadedURL
break
case .False:
finalHeaderImageURL = self.oldHeaderImageInDB
break
}
print(finalHeaderImageURL)
var finalProfileImageURL = String()
switch self.selectedProfileImage {
case .True:
finalProfileImageURL = self.storageProfileDownloadedURL
break
case .False:
finalProfileImageURL = self.oldProfilePhotoImageInDB
break
}
print(finalProfileImageURL)
guard let newDisplayName = self.displayNameTextField.text else { return }
guard let newLocation = self.locationTextField.text else { return }
guard let newDescription = self.bioTextField.text else { return }
guard let newWebsite = self.websiteTextField.text else { return }
guard let newBirthday = self.birthdayTextField.text else { return }
let newUpdatedUserDictionary = ["imageProfile": finalProfileImageURL,
"imageHeader" : finalHeaderImageURL,
"description" : newDescription,
"location": newLocation,
"displayName": newDisplayName,
"website": newWebsite,
"birthday": newBirthday,
]
Database.dataService.updateUserProfile(uid: userID, user: newUpdatedUserDictionary)
showAlert(title: "Hey", msg: "Your profile was updated", actionButton: "OK", viewController: self)
} // Get new uploaded profile and header image URLs
}
}
我在 switch 语句中使用枚举来确定它是旧 URL 还是新 URL:
enum SelectedHeaderImage
{
case True
case False
}
enum SelectedProfileImage
{
case True
case False
}
类网点:
var storageProfileDownloadedURL = String()
var storageHeaderDownloadedURL = String()
var oldProfilePhotoImageInDB = String()
var oldHeaderImageInDB = String()
var selectedHeaderImage = SelectedHeaderImage.False
var selectedProfileImage = SelectedProfileImage.False`
最佳答案
据我了解,您的问题出在排队上。您希望下面的代码在下载完成后执行,但它会按正常流程执行。如果这就是您的问题,那么我建议您创建另一个具有三个下载状态/计数的 enum
。并将该代码移动到您希望稍后在函数中执行的下方。下载完成后增加新 enum
的状态。它看起来像这样:
enum DownloadCount
{
case Zero
case One
case Two
}
var downloadCount = DownloadCount.Zero
在您下载完成的每个成功 block 中,将其更改为,我将在这里写一个,让您了解需要做什么。
if let profileImgDownloadedURL = url?.absoluteString
{
self.storageProfileDownloadedURL = profileImgDownloadedURL
print(self.storageProfileDownloadedURL)
self.selectedProfileImage = .True
if(downloadCount == .Zero)
{
downloadCount = DownloadCount.One
}
else
{
downloadCount = DownloadCount.Two
}
self.newAssigningFunction()
}
func newAssigningFunction()
{
if(downloadCount == .Two)
{
//Do your storage/saving work here
}
}
此外,如果您需要再次执行此函数,最好在 updateUserProfile
函数开始时将 downloadCount
设置回 Zero
.如果不清楚或您需要进一步帮助,请告诉我。或者,如果这不是您的情况。
关于ios - 在 Firebase 返回结果后为变量赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44902278/