"只能在写入事务中添加,删除或创建Realm中的对象"

时间:2016-07-28 20:46:05

标签: ios swift firebase realm

我试图在我的应用中使用Realm。我保存了数据并且一切都很好,但是当我想最终阅读并将其发送到Firebase时,我收到了错误。

@IBAction func registerButtonTapped(sender: BouncingButton) {
    if !monday.isEmpty() && !tuesday.isEmpty() && !wednesday.isEmpty() && !thursday.isEmpty() && !friday.isEmpty() && !saturday.isEmpty() && !sunday.isEmpty() {
        Constants.ActivityIndicator.startActivity()

        self.days.setObject(["open":monday.open,"close":monday.close], forKey: "Monday")
        self.days.setObject(["open":tuesday.open,"close":tuesday.close], forKey: "Tuesday")
        self.days.setObject(["open":wednesday.open,"close":wednesday.close], forKey: "Wednesday")
        self.days.setObject(["open":thursday.open,"close":thursday.close], forKey: "Thursday")
        self.days.setObject(["open":friday.open,"close":friday.close], forKey: "Friday")
        self.days.setObject(["open":saturday.open,"close":saturday.close], forKey: "Saturday")
        self.days.setObject(["open":sunday.open,"close":sunday.close], forKey: "Sunday")

        let email = Constants.clubRegisterForm.email
        let password = Constants.clubRegisterForm.password
        let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)

        Constants.FirebaseAPI.Auth?.createUserWithEmail(email, password: password, completion: { (user, error) in
            if error != nil {
                Constants.ActivityIndicator.stopActivity()
                Constants.Alerts.checkErrors(error!)
            } else {
                let changeRequest = user?.profileChangeRequest()
                changeRequest?.photoURL = NSURL(string: "\(Constants.FirebaseAPI.CLUB_PICS)/\(self.uniqueCode)")
                changeRequest?.commitChangesWithCompletion({ (error) in
                    if error != nil {
                        Constants.ActivityIndicator.stopActivity()
                        Constants.Alerts.checkErrors(error!)
                    } else {
                        Constants.FirebaseAPI.Auth?.signInWithCredential(credential, completion: { (user, error) in
                            if error != nil {
                                Constants.ActivityIndicator.stopActivity()
                                Constants.Alerts.checkErrors(error!)
                            } else {
                                do {
                                    let realm = try Realm()
                                    try realm.write {
                                        let userDataFromRealm = realm.objectForPrimaryKey(ClubRegisterForm.self, key: 1)!
                                        let userData: [String:AnyObject] = ["clubName":userDataFromRealm["name"]!,"email":userDataFromRealm["email"]!,"address":userDataFromRealm["address"]!,"price":userDataFromRealm["price"]!,"phoneNumber":userDataFromRealm["phoneNumber"]!,"numberOfCourts":userDataFromRealm["numberOfCourts"]!,"bankAccount":userDataFromRealm["bankAccount"]!,"accountHolder":userDataFromRealm["accountHolder"]!,"branchNumber":userDataFromRealm["branchNumber"]!]

                                        Constants.FirebaseAPI.REF_USERS_CLUBS.child("\(self.uniqueCode)").setValue(userData, withCompletionBlock: { (error, reference) in
                                            if error != nil {
                                                Constants.ActivityIndicator.stopActivity()
                                                Constants.Alerts.checkErrors(error!)
                                            } else {

                                                Constants.FirebaseAPI.REF_USERS_CLUBS.child("\(self.uniqueCode!)").updateChildValues(["OpenHours":self.days,"services":self.clubServices])
                                                Constants.ActivityIndicator.stopActivity()
                                                realm.delete(Constants.clubRegisterForm)
                                                self.performSegueWithIdentifier(Constants.Segues.registered, sender: nil)

                                            }
                                        })
                                    }
                                } catch {
                                    Constants.Alerts.errorOccured()
                                }
                            }
                        })
                    }
                })
            }
        })
    } else {
        Constants.Alerts.emptyFields()

    }
}

这是我收到的错误:

  

由于未捕获的异常终止应用' RLMException',原因:'可以   仅在写入事务中在Realm中添加,删除或创建对象    - 首先在RLMRealm实例上调用beginWriteTransaction。'

它说我在这个功能中做错了什么,但是我没有在Realm中添加,创建或删除任何内容,只是阅读。

解决方案

我犯了一个愚蠢的错误,我试图在写作之外做领域。我忘记了它

0 个答案:

没有答案