【问题标题】:dispatch_group_leave error while adding new child添加新子时 dispatch_group_leave 错误
【发布时间】:2017-04-09 07:13:00
【问题描述】:

我正在尝试使用以下代码向我的 firebase 节点添加一个新子节点:

@IBAction func likeButtonOnTouch(_ sender: Any) {

    if ViewController.usersUid.count > 0 {

        self.update()

    }
}

func update() {
    let child1: String = (FIRAuth.auth()?.currentUser?.uid)!
    let ref = FIRDatabase.database().reference().child(child1).child("following")


    let data1: [String:String] = [ViewController.usersUid[self.currentUser]: "true"]

    ref.setValue(data1)
}

但是当我按下按钮时,我的应用程序崩溃并出现以下错误:

线程 1 exc_bad_instruction(代码=1 子代码=0x100ef5c78)

在这一行:

ref.setValue(data1)

我不知道这意味着什么以及如何解决它。该值已成功添加到我的数据库中的最有趣的事情(!!!),但是应用程序崩溃了。请给我一个建议。

附:崩溃报告:

* thread #1: tid = 0xb503, 0x0000000100f11c78 libdispatch.dylib`dispatch_group_leave + 76, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x100f11c78)
frame #0: 0x0000000100f11c78 libdispatch.dylib`dispatch_group_leave + 76
frame #1: 0x00000001000a36f8 Elite Club`thunk + 68 at ViewController.swift:0
frame #2: 0x00000001001bf8a4 Elite Club`__43-[FValueEventRegistration fireEvent:queue:]_block_invoke.57((null)=<unavailable>) + 88 at FValueEventRegistration.m:60 [opt]
frame #3: 0x0000000100f0d258 libdispatch.dylib`_dispatch_call_block_and_release + 24
frame #4: 0x0000000100f0d218 libdispatch.dylib`_dispatch_client_callout + 16
frame #5: 0x0000000100f12280 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1200
frame #6: 0x000000019376e810 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
frame #7: 0x000000019376c3fc CoreFoundation`__CFRunLoopRun + 1660
frame #8: 0x000000019369a2b8 CoreFoundation`CFRunLoopRunSpecific + 444
frame #9: 0x000000019514e198 GraphicsServices`GSEventRunModal + 180
frame #10: 0x00000001996e17fc UIKit`-[UIApplication _run] + 684
frame #11: 0x00000001996dc534 UIKit`UIApplicationMain + 208
frame #12: 0x00000001000c04b8 Elite Club`main + 140 at AppDelegate.swift:15
frame #13: 0x000000019267d5b8 libdyld.dylib`start + 4

【问题讨论】:

  • ref 为零或data1 为零?
  • @Paulw11,没有。我已经通过在 xcode 控制台中打印值来检查这一点,并且数据已成功添加到我的数据库中的 ref
  • @NazmulHasan Hasan 同样的错误
  • 问题是这段代码 ViewController.usersUid[self.currentUser]。如果我将您的代码复制并粘贴到一个项目中,它会在删除该代码的情况下运行良好。上面的设置有点奇怪.. self.currentUser 是什么,为什么将它存储在 ViewController 的数组 var 中?
  • 我尝试将值更改为其他,但结果相同

标签: ios swift firebase swift3 firebase-realtime-database


【解决方案1】:

如果ViewController.usersUid[self.currentUser]不存在请崩溃

你可以试试这个方法吗:

let data1: [String:String] = [(ViewController.usersUid[self.currentUser] ?? "") : "true"]

ref.setValue(data1) { (error, ref) -> Void in

}

更清洁的方式:

if let currentuser =  ViewController.usersUid[self.currentUser]  {

    let data1: [String:String] = [currentuser : "true"]

    ref.setValue(data1) { (error, ref) -> Void in

    }

}

【讨论】:

  • 是的。我还检查了它没有与 ViewController.usersUid[self.currentUser] 连接的错误
  • 我曾尝试将此代码粘贴到另一个 VC 中,但结果是一样的。似乎 setValue 方法工作不正确,我真的不知道为什么,因为我已经在另一个 VC 中使用过这个方法并且效果很好。
【解决方案2】:

经过一天的工作,我想通了。首先,您应该退出,然后再登录一次。这是我的最终代码:

func update() {

        do {
            let ref = try FIRAuth.auth()?.signOut()
        }
        catch {}

        let preferences = UserDefaults.standard
        FIRAuth.auth()?.signIn(withEmail: preferences.object(forKey: "userName") as! String, password: preferences.object(forKey: "userPassword") as! String) { (user, error) in

            if error == nil {
                LoggingIn.userUID = (user?.uid)!

                print(user?.uid ?? "not found")
                print("You have successfully logged in")

                let child1: String = (FIRAuth.auth()?.currentUser?.uid)!
                let ref = FIRDatabase.database().reference().child(child1).child("following")


                let data1: [String:String] = [ViewController.usersUid[self.currentUser] : "true"]



                ref.setValue(data1) { (error, ref) -> Void in

                }

            } else {


            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2018-11-22
    相关资源
    最近更新 更多