在异步任务中快速返回Firebase [英] swift Firebase return in asynchron Task

查看:74
本文介绍了在异步任务中快速返回Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift 2中遇到了iOS版Firebase SDK的问题.我试图将图片设置为从Firebase存储下载的图片.当我调用该函数时,它会返回nil.我认为是因为Firebase sdk提供的下载任务是异步的,所以当返回状态意为uid时,由于任务尚未完成,因此未设置必要的uid.我该如何解决这个问题,以便获得正确的图片?

I have a problem with the Firebase SDK for iOS in swift 2. Im trying to set a picture to a downloaded from the Firebase storage. When I call the function it gives back nil. I think its because the download task provided by the Firebase sdk is asynchron, so when the return state meant is called the uid which is necessary isn't set because the Task isn't finished. How can I solve this so I get the right picture returned?

override func viewDidLoad() {
   super.viewDidLoad()
   imageView.image = downloadProfilePicFirebase()
}

Firebase下载功能:

The Firebase download function:

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

推荐答案

Firebase是您所提到的异步的,因此,它可以驱动应用程序中的数据流.

Firebase is asynchronous as you mentioned so let it drive the flow of data within your app.

请勿尝试从Firebase块返回数据-让该块处理返回的数据,然后在该数据在该块内有效后移至下一步.

Don't try to return data from a Firebase block - let the block handle the returned data and then move to the next step once that data is valid within the block.

有两种选择:

一种选择是从.writeToFile完成处理程序填充数据

One option is to populate your data from the .writeToFile completion handler

override func viewDidLoad() {
   super.viewDidLoad()
   downloadPic()
}

func downloadPic {
    let download = profilePicRef.writeToFile(localURL) { (URL, error) -> Void in
      if (error != nil) {
        // handle an error
      } else {
        imageView.image = UIImage(...
        //then update your tableview, start a segue, or whatever the next step is
      }
    }
}

第二个选项是将观察者添加到节点,完成后,填充您的imageView

A second option is add an observer to the node and when that completes, populate your imageView

override func viewDidLoad() {
   super.viewDidLoad()

   let download = storageRef.child('your_url').writeToFile(localFile)

   let observer = download.observeStatus(.Success) { (snapshot) -> Void in
     imageView.image = UIImage(...
     //then update your tableview, start a segue, or whatever the next step is
   }
}

这篇关于在异步任务中快速返回Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆