【问题标题】:Xcode firebase simply write dataXcode firebase 简单写数据
【发布时间】:2018-04-30 21:07:46
【问题描述】:

我编写此代码是因为我需要一个带有 2 个按钮的应用程序,当我按下第一个按钮时,我想在 FirebaseDB 中编写 video01,当我按下第二个按钮时,我想编写 video02。 当我编译代码时,我没有看到这两个按钮,并且在我看来是 errore SIGABRT

这是我的代码:

import Firebase
import UIKit

class ViewController: UIViewController {

    var refController: DatabaseReference!

    @IBAction func btt_video1(_ sender: Any) {
        refController.child("nowPlaying").setValue("video01")
    }
    @IBAction func btt_video2(_ sender: Any) {
        refController.child("nowPlaying").setValue("video02")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //configuring firebase

        FirebaseApp.configure()

        refController = Database().reference().child("parametri");
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

这是我的错误:

2018-04-30 16:31:42.403230-0400 Remote Controller Client[26074:469999] 4.13.0 - [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40200000 started
2018-04-30 16:31:42.407953-0400 Remote Controller Client[26074:469999] 4.13.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled 
2018-04-30 16:31:42.444269-0400 Remote Controller Client[26074:469864] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKeyedSubscript:]: key cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f2d01e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x000000010e3d0031 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010f3100bc _CFThrowFormattedException + 194
    3   CoreFoundation                      0x000000010f3746e2 -[__NSDictionaryM setObject:forKeyedSubscript:] + 1026
    4   Remote Controller Client            0x000000010c857470 +[FRepoManager createRepo:config:database:] + 273
    5   Remote Controller Client            0x000000010c83eb16 -[FIRDatabase ensureRepo] + 151
    6   Remote Controller Client            0x000000010c83e20e -[FIRDatabase reference] + 31
    7   Remote Controller Client            0x000000010c7b6da3 _T024Remote_Controller_Client04ViewB0C11viewDidLoadyyF + 131
    8   Remote Controller Client            0x000000010c7b6f44 _T024Remote_Controller_Client04ViewB0C11viewDidLoadyyFTo + 36
    9   UIKit                               0x000000010f8f1191 -[UIViewController loadViewIfRequired] + 1215
    10  UIKit                               0x000000010f8f15d4 -[UIViewController view] + 27
    11  UIKit                               0x000000010f7bf183 -[UIWindow addRootViewControllerViewIfPossible] + 122
    12  UIKit                               0x000000010f7bf894 -[UIWindow _setHidden:forced:] + 294
    13  UIKit                               0x000000010f7d262c -[UIWindow makeKeyAndVisible] + 42
    14  UIKit                               0x000000010f74643a -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4739
    15  UIKit                               0x000000010f74b62b -[UIApplication _runWithMainScene:transitionContext:completion:] + 1677
    16  UIKit                               0x000000010fb0de4a __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 866
    17  UIKit                               0x000000010fee0909 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    18  UIKit                               0x000000010fb0da86 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
    19  UIKit                               0x000000010fb0e2a7 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 675
    20  UIKit                               0x000000011047f4d4 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
    21  UIKit                               0x000000011047f36e -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
    22  UIKit                               0x000000011016362d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    23  UIKit                               0x000000011035e387 _performActionsWithDelayForTransitionContext + 100
    24  UIKit                               0x00000001101634f7 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
    25  UIKit                               0x000000010fedffb0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    26  UIKit                               0x000000010f749f0c -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 515
    27  UIKit                               0x000000010fd1ca97 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
    28  FrontBoardServices                  0x00000001163932f3 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 331
    29  FrontBoardServices                  0x000000011639bcfa __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 225
    30  libdispatch.dylib                   0x0000000111ee7848 _dispatch_client_callout + 8
    31  libdispatch.dylib                   0x0000000111eece14 _dispatch_block_invoke_direct + 592
    32  FrontBoardServices                  0x00000001163c7470 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    33  FrontBoardServices                  0x00000001163c712e -[FBSSerialQueue _performNext] + 439
    34  FrontBoardServices                  0x00000001163c768e -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    35  CoreFoundation                      0x000000010f272bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    36  CoreFoundation                      0x000000010f2574af __CFRunLoopDoSources0 + 271
    37  CoreFoundation                      0x000000010f256a6f __CFRunLoopRun + 1263
    38  CoreFoundation                      0x000000010f25630b CFRunLoopRunSpecific + 635
    39  GraphicsServices                    0x0000000115019a73 GSEventRunModal + 62
    40  UIKit                               0x000000010f74d0b7 UIApplicationMain + 159
    41  Remote Controller Client            0x000000010c7b8497 main + 55
    42  libdyld.dylib                       0x000000010f103955 start + 1
    43  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

有人可以帮助我吗??

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    setValue 接受字典

    let video1 = ["Video":"Video1"]
    refController.child("nowPlaying").setValue(video1)
    

    let video2 = ["Video":"Video2"]
    refController.child("nowPlaying").setValue(video2)
    

    【讨论】:

    • 马克 - 答案不正确。 .setValue 的参数可以是 String、Int、字典甚至是 Array。在您的回答中,键:值对将被创建为 nowPlaying 的子节点。但是,使用 .child("nowPlaying).setValue("Some Video") 会将 key: value 对设置为 nowPlaying: Some Video
    • 我没说它不需要字符串,你自己说它需要一个字典,你可以将我提供的视频密钥从 video 更改为 nowPlaying 并在 refController 节点上进行更新这将产生完全相同的效果。 (没有额外的节点创建)。对单个键使用 setValue 不是理想的 IMO 编程,它会消除代码的一致性,而您总是会传递字典来保存数据块。
    • 您的回答表明 setValue 需要一个字典,这是不正确的,至少不完全正确。关键是我们不希望未来的读者认为这是唯一的选择。如果您查看 OP 的问题,真正的问题是他们在某些时候写 nil 是无效的。
    • 好吧,我同意现在重新阅读它。对数据库的引用不存在并且返回 nil。
    【解决方案2】:

    问题是,在某些时候,nil 被写入不允许的地方和/或 Firebase 配置不正确。

    一个重要的故障排除步骤是代码无法编译,这意味着 Firebase 可能未正确配置。

    看两件事:

    1) 您没有正确初始化数据库引用。

    你拥有的是

    refController = Database().reference().child("parametri");

    应该是的

    self.refController = Database.database().reference()
    

    然后,你可以在后面的代码中做

    let paraRef = self.refController.child("parametri")
    let nowPlayingRef = paraRef.child("nowPlaying")
    nowPlayingRef.setValue("video 1")
    

    这将导致这样的结构

    firebase_root
       parametri
          nowPlaying: "video 1"
    

    2) 在您的 App Delegate 中执行此操作

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
       var window: UIWindow?
       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
           FirebaseApp.configure()
           return true
       }
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2017-08-16
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2018-09-21
      • 2018-02-26
      • 2014-03-21
      相关资源
      最近更新 更多