Swift错误:NSInternalInconsistencyException,'试图在非主队列上执行的上传!仅在主队列上。'

时间:2018-08-15 14:32:51

标签: ios swift facebook firebase twitter

我正在我的应用程序(使用TwitterKit)上实现使用Twitter选项登录,但在以下功能上不断崩溃-saveUserIntoFirebaseDatabase(将用户数据,图像上传到Firebase数据库时)。我不明白为什么,或者为什么下面的函数需要在主队列中?

已提取用户数据并将其保存到Firebase Auth部分,但是之后尝试将用户数据保存到Realtime Database&Storage时似乎崩溃了?

fileprivate func saveUserIntoFirebaseDatabase() {

    guard let uid = Auth.auth().currentUser?.uid,
    let name = self.name,
    let username = self.username,
    let email = self.email,
    let profileImage = profileImage,
    let profileImageUploadData = UIImageJPEGRepresentation(profileImage, 0.3) else { Service.dismissHud(self.hud, text: "Error", detailText: "Failed to save user.", delay: 3); return }
    let filename = UUID().uuidString

    let storageRef = Storage.storage().reference().child("profile_images").child(filename)
    storageRef.putData(profileImageUploadData, metadata: nil, completion: { (metadata, err) in

        if let err = err {
            Service.dismissHud(self.hud, text: "Error", detailText: "Failed to save user with error: \(err)", delay: 3);
            return
        }

        // Firebase 5 Update: Must now retrieve downloadURL
        storageRef.downloadURL(completion: { (downloadURL, err) in
        guard let profileImageUrl = downloadURL?.absoluteString else { return }
        print("Successfully uploaded profile image into Firebase storage with URL:")

        let dictionaryValues = ["name": name,
                                "email": email,
                                "username": username,
                                "profileImageUrl": profileImageUrl]
        let values = [uid : dictionaryValues]

        Database.database().reference().child("users").updateChildValues(values, withCompletionBlock: { (err, ref) in
            if let err = err {
                Service.dismissHud(self.hud, text: "Error", detailText: "Failed to save user info with error: \(err)", delay: 3)
                return
            }
            print("Successfully saved user info into Firebase database")
            // after successfull save dismiss the welcome view controller
            self.hud.dismiss(animated: true)
            self.dismiss(animated: true, completion: nil)
            })
        })
    })
} 

我已设置的MainTabBarController具有以下功能,以查看当前用户是否已登录,如果未登录,则将其重定向到Welcome Controller。这可能是原因吗?

fileprivate func checkLoggedInUserStatus() {
    if Auth.auth().currentUser == nil {
        DispatchQueue.main.async {
            let welcomeController = WelcomeController()
            let welcomeNavigationController = UINavigationController(rootViewController: welcomeController)
            self.present(welcomeNavigationController, animated: false, completion: nil)
            return
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试关闭后台线程上的viewcontroller。