Firebase iOS用户详细信息存储

时间:2018-08-02 13:01:21

标签: ios swift firebase firebase-storage

我有一个用户在其中注册的应用程序,并且将Firebase用于后端存储和数据库。一切正常,直到我决定将用户个人资料图像保存到存储中并将字符串保存到数据库。每当我单击“注册”按钮时,都会注册用户,但不会将详细信息保存到数据库中,也不会将图像保存到存储中。在添加用于存储个人资料图像的选项之前,用户详细信息已完美地保存在数据库中,直到我实现了存储,并且单击注册按钮几秒钟后应用程序崩溃了。

StorageProvider

var storeageRef: StorageReference{
        return Storage.storage().reference(forURL: "firebase/xxxx")
    }

    //MARK: - User's Child
    var profilePicsRef : StorageReference {
        return storeageRef.child(Constants.PROFILE_IMAGE)
    }
    func saveProfile(withID: String, username: String, email: String, userImage: UIImage) -> Void {

        profilePicsRef.child(withID)
        if let profileImg: UIImage = userImage, let imgData = UIImageJPEGRepresentation(profileImg, 0.1) {

            storeageRef.putData(imgData, metadata: nil, completion: { (metaData, error) in

                if error != nil {
                    return
                }
                guard let userImgUrl = metaData?.downloadURL()?.absoluteString else {return}
                DatabaseProvider.Instance.saveUser(withID: withID, username: username, email: email, userImgUrl: userImgUrl)

            })
        }
    }

DatabaseProvider

//MARK: - Database Reference
    var dbRef: DatabaseReference{
        return Database.database().reference()
    }

    //MARK: - User's Child
    var usersRef : DatabaseReference {
        return dbRef.child(Constants.USERS)
    }

    func saveUser(withID: String, username: String, email: String, userImgUrl: String) -> Void {

        let userData: Dictionary<String, Any> = [Constants.USERNAME: username, Constants.EMAIL: email, Constants.PROFILE_IMAGE: userImgUrl]

        usersRef.child(withID).setValue(userData)

    }

AuthProvider

   //MARK: - Sign Up Function
    func signUp(username: String, withEmail email: String, password: String,          userImg: UIImage, loginHandler: LoginHandler?) -> Void {
    var imgUrl: String = ""
    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        if (error != nil) {
            self.handleError(error: error! as NSError, loginHandler: loginHandler)
        } else {
            guard let uid = user?.uid else {return}


            if (user != nil) {

                StorageProvider.Instance.saveProfile(withID: uid, username: username, email: email, userImage: userImg)

                self.login(withEmail: email, password: password, loginHandler: loginHandler)
            }

            loginHandler?(nil)
        }
    }

  }

错误日志

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000112f2d12b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00000001125c1f41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000112f6d0cc _CFThrowFormattedException + 194
    3   CoreFoundation                      0x0000000112e41951 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 321
    4   CoreFoundation                      0x0000000112e417db +[NSDictionary dictionaryWithObjects:forKeys:count:] + 59
    5   xxx                          0x000000010ff8f4cf -[FIRStorageUploadTask enqueue] + 800
    6   xxx                          0x000000010ff8b4df -[FIRStorageReference putData:metadata:completion:] + 845
    7   xxx                          0x000000010fe0d6d7 _T010xxx15StorageProviderC11saveProfileySS6withID_SS8usernameSS5emailSo7UIImageC9userImagetF + 1079
    8   xxx                          0x000000010fdca5b3 _T010xxx12AuthProviderC6signUpySS8username_SS9withEmailSS8passwordSo7UIImageC7userImgySSSgcSg12loginHandlertFySo4UserCSg_s5Error_pSgtcfU_ + 1235
    9   xxx                          0x000000010fdcb5b9 _T010xxx12AuthProviderC6signUpySS8username_SS9withEmailSS8passwordSo7UIImageC7userImgySSSgcSg12loginHandlertFySo4UserCSg_s5Error_pSgtcfU_TA + 329
    10  xxx                          0x000000010fdc9aac _T0So4UserCSgs5Error_pSgIxxx_ACSo7NSErrorCSgIyByy_TR + 124
    11  libdispatch.dylib                   0x0000000116b1b2f7 _dispatch_call_block_and_release + 12
    12  libdispatch.dylib                   0x0000000116b1c33d _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x0000000116b275f9 _dispatch_main_queue_callback_4CF + 628
    14  CoreFoundation                      0x0000000112eefe39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    15  CoreFoundation                      0x0000000112eb4462 __CFRunLoopRun + 2402
    16  CoreFoundation                      0x0000000112eb3889 CFRunLoopRunSpecific + 409
    17  GraphicsServices                    0x0000000117c049c6 GSEventRunModal + 62
    18  UIKit                               0x0000000113d5d5d6 UIApplicationMain + 159
    19  xxx                          0x000000010fe26577 main + 55
    20  libdyld.dylib                       0x0000000116b98d81 start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000112f2d12b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00000001125c1f41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000112f6d0cc _CFThrowFormattedException + 194
    3   CoreFoundation                      0x0000000112e41951 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 321
    4   CoreFoundation                      0x0000000112e417db +[NSDictionary dictionaryWithObjects:forKeys:count:] + 59
    5   xxx                          0x000000010ff8f4cf -[FIRStorageUploadTask enqueue] + 800
    6   xxx                          0x000000010ff8b4df -[FIRStorageReference putData:metadata:completion:] + 845
    7   xxx                          0x000000010fe0d6d7 _T010xxx15StorageProviderC11saveProfileySS6withID_SS8usernameSS5emailSo7UIImageC9userImagetF + 1079
    8   xxx                          0x000000010fdca5b3 _T010xxx12AuthProviderC6signUpySS8username_SS9withEmailSS8passwordSo7UIImageC7userImgySSSgcSg12loginHandlertFySo4UserCSg_s5Error_pSgtcfU_ + 1235
    9   xxx                          0x000000010fdcb5b9 _T010xxx12AuthProviderC6signUpySS8username_SS9withEmailSS8passwordSo7UIImageC7userImgySSSgcSg12loginHandlertFySo4UserCSg_s5Error_pSgtcfU_TA + 329
    10  xxx                          0x000000010fdc9aac _T0So4UserCSgs5Error_pSgIxxx_ACSo7NSErrorCSgIyByy_TR + 124
    11  libdispatch.dylib                   0x0000000116b1b2f7 _dispatch_call_block_and_release + 12
    12  libdispatch.dylib                   0x0000000116b1c33d _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x0000000116b275f9 _dispatch_main_queue_callback_4CF + 628
    14  CoreFoundation                      0x0000000112eefe39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    15  CoreFoundation                      0x0000000112eb4462 __CFRunLoopRun + 2402
    16  CoreFoundation                      0x0000000112eb3889 CFRunLoopRunSpecific + 409
    17  GraphicsServices                    0x0000000117c049c6 GSEventRunModal + 62
    18  UIKit                               0x0000000113d5d5d6 UIApplicationMain + 159
    19  xxx                          0x000000010fe26577 main + 55
    20  libdyld.dylib                       0x0000000116b98d81 start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

0 个答案:

没有答案