swift - 自动从 Firebase 数据库中删除数据

标签 swift xcode firebase firebase-realtime-database

我看到了一些其他问题,但我无法使其正常工作。我有一个用 swift 编码的 Mac 应用程序,它有一个 Firebase 登录,但用户输入一个存储在 Firebase 上的 key ,有没有办法在用户成功使用该 key 后自动删除该 key ?

这是我的数据库。

datbase

这是当前使用的代码。

import Cocoa
import FirebaseAuth
import FirebaseDatabase

class LoginViewController: NSViewController {

    @IBOutlet weak var textUsername: NSTextField!
    @IBOutlet weak var textPassword: NSSecureTextFieldCell!
    @IBOutlet weak var btnLogin: NSButton!
    var keyArray = \[Int64\]()

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    override func viewWillAppear() {
    }
    func getLoginState() -> Bool{

        let state = UserDefaults.standard.bool(forKey: "isRegistered")
        if (state) {
            return true
        } else {
            return false
        }

    }
    override func viewDidAppear() {

        let state = self.getLoginState()
        if (state){
            self.performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "loginsegue"), sender: nil)
            self.view.window?.close()
        }

        var ref: DatabaseReference!
        ref = Database.database().reference()
        let keyRef = ref.child("key1")

        keyRef.observe(DataEventType.childAdded, with: { (snapshot) in
            //    let postDict = snapshot.value as? \[String : AnyObject\] ?? \[:\]
            let keyStr = snapshot.value as? Int64
            if let actualPost = keyStr{
                self.keyArray.append(actualPost)
            }
        })
    }

    @IBAction override func dismissViewController(_ viewController: NSViewController) {
        dismiss(self)
    }
    @IBAction func close(sender: AnyObject) {
        self.view.window?.close()
    }
    @IBAction func onSignup(_ sender: Any) {
//        self.performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "gotosignup"), sender: sender)
//        self.view.window?.close()

    }
    func dialogOK(question: String, text: String) -> Void {
        let alert: NSAlert = NSAlert()
        alert.messageText = question
        alert.informativeText = text
        alert.alertStyle = NSAlert.Style.warning
        alert.addButton(withTitle: "OK")
        alert.runModal()
    }

    @IBAction func onLogin(_ sender: Any) {
        //self.btnLogin.isEnabled = false
        var isKey = false
        if (!self.textUsername.stringValue.isEmpty) {
            for key in keyArray{
                if(Int64(self.textUsername.stringValue)! == key)
                {
                    UserDefaults.standard.set(true, forKey:"isRegistered")

                    self.performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "loginsegue"), sender: nil)
                    self.view.window?.close()
                    isKey = true
                }
            }
            if (!isKey){
                self.dialogOK(question: "Error", text: "Invalid Key")
            }
        } else {
            self.dialogOK(question: "Error", text: "Please Input Key")
        }

    }
}

最佳答案

你不能像这样对数据库进行排序并期望有一个有效的代码,即使有任何代码。它会产生困惑的代码:

You need to:

  • Sort your database like [1220:0]. the key first. 0 & 1 as an indicator if it's used or not.
  • Once the user taps onLogin() you need to set the used key value to 1
  • Setup Cloud Functions to check if the used key is equal to 1, if yes. then remove the key.

Do the rest of the work.

帮助您入门的相关文章:

关于swift - 自动从 Firebase 数据库中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52789548/

相关文章:

javascript - 使用规则强制 Firebase 数据库中字段的用户 ID

android - 编译 'com.firebaseui:firebase-ui:1.0.1' 显示错误

iOS 一键两个标签

ios - 从共享扩展中检索 imageURL

c - 为什么这两个 C 语句产生相同的输出

objective-c - Cocoa ConnectionKit 框架依赖项

ios - UIAlertController .actionSheet 项目之间的分隔线不一样

ios - swift 将数据发送到Apple Watch

ios - 如何在 URLSession 的 HTTP header 中为 'Token xxxxxxxxxx' 设置 'Authorization'

java - 我试图通过完成子节点来返回列表