Swift Firebase如何停止进程?

时间:2016-08-20 16:08:12

标签: swift firebase firebase-realtime-database firebase-storage

class ProfileViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var btnMenu: UIBarButtonItem!

@IBOutlet weak var image1: UIImageView!

@IBOutlet weak var txtMessage: UILabel!

var imagePicker = UIImagePickerController()

var rootRef = FIRDatabase.database().reference()

var storage = FIRStorage.storage()

var storageRef = FIRStorage.storage().referenceForURL("gs://...")   

@IBAction func actionLoad(sender:AnyObject){

    var count : Int?
    var fileName : String?

   let imageRef = self.rootRef.child("users").child(LoginViewController.USER_ID!).child("images").observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
        count = snapshot.value! as! Int


        var data = NSData()
        data = UIImageJPEGRepresentation(self.image1.image!, 0.8)!

        if count == 0 {
            fileName = "1.jpg"
        }else if count == 1 {
            fileName = "2.jpg"
        }else if count == 2 {
            fileName = "3.jpg"
        }else if count == 3 {
            fileName = "4.jpg"
        }else if count == 4 {
            fileName = "5.jpg"
        }
        let metaData = FIRStorageMetadata()
        metaData.contentType = "image/jpg"

        if count < 5 {

            self.storageRef.child(LoginViewController.USER_ID!).child(fileName!).putData(data, metadata: metaData){(metaData,error) in
                if let error = error {

                    self.txtMessage.text = error.localizedDescription

                }else{

                    self.txtMessage.text = "Your picture uploaded"
                    self.rootRef.child("users").child(LoginViewController.USER_ID!).child("images").setValue(count!+1)

                }

            }

        }else{
            self.txtMessage.text = "Sorry, you may to upload maximum 5 photos"

        }

    })

}

如何停止?它一直持续到count == 5.但是不想循环过程。点击后它应该上传一张图片并停止!我试过了 : “rootRef.removeAllObservers”但不确定如何使用它

1 个答案:

答案 0 :(得分:0)

点击按钮&#34; actionLoad&#34;功能检查已经上传了多少张图片,如果少于5张图片,则功能上传用户选择的图片。但是通过一键单击按钮功能不会停止并连续上传该图片直到5上传的问题。