ios - Swift Firebase 持久化

标签 ios swift firebase firebase-realtime-database runtime-error

我正在尝试启用 Firebase Persistence,但我的代码一直崩溃:

terminating with uncaught exception of type NSException

我已经尝试了我的 viewDidLoad 和 DataService 下的代码行,但我仍然遇到同样的崩溃。我需要做什么来解决我面临的这个问题

import UIKit
import Firebase

class HomeTeamSelectionVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var club: Clubs!
    var player = [Players]()
    var playerFirstName = String()
    var playerLastName = String()
    var playerSelected: Bool = false


    override func viewDidLoad() {
        super.viewDidLoad()

        FIRDatabase.database().persistenceEnabled = true //Correct use of????

        CLUB_KEY = ""
        CLUB_KEY = club.clubKey

        tableView.dataSource = self
        tableView.delegate = self


        DataService.ds.REF_PLAYERS.queryOrdered(byChild: "clubKey").queryEqual(toValue: club.clubKey).observe(.value, with: { (snapshot) in

            print("PLAYERS_COUNT: \(snapshot.childrenCount)")
            print("PLAYERS_SNAPSHOT: \(snapshot)")

            self.player = []
            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                for snap in snapshots {
                    if let playerDict = snap.value as? Dictionary<String, AnyObject> {
                        let key = snap.key

                        let players = Players(playerKey: key, dictionary: playerDict)
                        self.player.append(players)

                    }
                }
            }
            //            self.tableView.reloadData()
        }) { (error) in
            print(error.localizedDescription)
            print("CHET: local error")
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return player.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let players = player[indexPath.row]
        if let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTeamPlayersCell") as? HomeTeamPlayersCell {
            cell.configureCell(players)
            return cell
        } else {
            return HomeTeamPlayersCell()
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let players: Players!
        players = player[indexPath.row]

        print (players.playerKey)
        print (players.playerFirstName)
        print (players.playerLastName)

        dismiss(animated: true, completion: nil)
    }
}

最佳答案

来自 Firebase documentation for persistenceEnabled property :

Note that this property must be set before creating your first Database reference and only needs to be called once per application.

因此,标准做法是在您的 AppDelegate 类中设置它一次。例如:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FIRApp.configure()
    FIRDatabase.database().persistenceEnabled = true
    ...
    return true

关于ios - Swift Firebase 持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44657447/

相关文章:

swift - 谷歌地图内存问题

java - 在我关闭应用程序并再次打开之前,数据不会添加到 Firebase 上

java - Firebase 查询 Or'ing whereEqualTo 以获得可能值的列表

ios - 如何获取我的 iOS 应用程序的本地化语言?

ios - ARC递归方法中的EXC_BAD_ACCESS代码= 2

ios - 如何解决错误 'No visible @interface for ' UIViewController' declares the selector ''

ios - Swift 3 - 在 UITableViewCell 中重新加载 UICollectionView

ios - Swift - UIImageView 缩放比 UIScrollView 大

firebase 仅部署功能无法识别我的任何功能

ios - 检查 Objective-C 字符串中的特定字符