-1
我在swift 2中的iOS版Firebase SDK存在問題。我試圖將圖片設置爲從Firebase存儲中下載的圖片。當我調用函數時,它返回零。我認爲它是因爲Firebase sdk提供的下載任務是異步的,所以當返回狀態意味着被稱爲必需的uid因爲任務尚未完成而未設置時。我如何解決這個問題,以便我得到正確的圖片?swift Firebase以異步方式返回任務
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = downloadProfilePicFirebase()
}
的火力地堡下載功能:
func downloadProfilePicFirebase() -> UIImage{
print("download called")
//local paths
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectorPath:String = paths[0]
let imagesDirectoryPath = documentDirectorPath.stringByAppendingString("/profiles")
var uid = String()
if let user = FIRAuth.auth()?.currentUser {
let uid = user.uid
let storageRef = FIRStorage.storage().referenceForURL("gs://myid.appspot.com")
let profilePicRef = storageRef.child("/profile_pic.jpg")
let homeDir: NSURL = NSURL.fileURLWithPath(NSHomeDirectory())
let fileURL: NSURL = homeDir.URLByAppendingPathComponent("Documents").URLByAppendingPathComponent("profiles").URLByAppendingPathComponent("profile_pic").URLByAppendingPathExtension("jpg")
// Download to the local filesystem
let downloadTask = profilePicRef.writeToFile(fileURL) { (URL, error) -> Void in
if (error != nil) {
print(error)
} else {
// svaed localy now put in ImageView
}
}
}
return UIImage(contentsOfFile: "\(imagesDirectoryPath)"+"/profile_pic_user_"+uid+".jpg")!
}
請參閱本【答案】(http://stackoverflow.com/a/38439609/5654848)。 – Dershowitz123
@ Dershowitz123是的,但返回的狀態意味着將在完成之前調用我想在這一點上保持功能 –
也看到這個問題,並參考我的答案:http://stackoverflow.com/questions/38547875/how -to-運行obverveeventtype功能於主線程 –