【问题标题】:How to check for value in Firebase that is held under a autoID child?如何检查在 AutoID 子项下持有的 Firebase 中的值?
【发布时间】:2016-11-29 22:03:41
【问题描述】:

点击删除按钮即可。我正在尝试从我的 Firebase 数据库中删除一个特定的孩子。我的问题是我无法在我的数据库中引用特定的childByAutoId,因此我的应用程序不知道要删除哪个孩子。

Firebase 数据库

数据服务文件

import Foundation
import Firebase
import UIKit

let DB_BASE = FIRDatabase.database().reference().child("laboratory")        //contains the root of our database
let STORAGE_BASE = FIRStorage.storage().reference()

class DataService {

static let ds = DataService()

//DB References
private var _REF_BASE = DB_BASE
private var _REF_STATION = DB_BASE.child("stations")
private var _REF_USERS = DB_BASE.child("users")


//Storage Reference
private var _REF_ITEM_IMAGE = STORAGE_BASE.child("item-pics")

var REF_BASE: FIRDatabaseReference {

    return _REF_BASE

}

var REF_STATION: FIRDatabaseReference {

    return _REF_STATION

}

var REF_USERS: FIRDatabaseReference {

    return _REF_USERS

}

var REF_ITEM_IMAGES: FIRStorageReference {

    return _REF_ITEM_IMAGE

}

//creating a new user into the firebase database

func createFirebaseDBUser(_ uid: String, userData: Dictionary<String, String>) {
    REF_USERS.child(uid).updateChildValues(userData)
}

}

我要删除的信息不在tableViewCell 中。我尝试将键附加到数组中,但我仍然无法访问要删除的相应子项。

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(tableView: (UITableView!), commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: (NSIndexPath!)) {

}


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let currentStation = station.title
    let stationRef = DataService.ds.REF_STATION.child(currentStation!)
    let inventoryRef = stationRef.child("inventory")

    var deleteAction = UITableViewRowAction(style: .default, title: "Delete") {action in

            //delete items from firebase


            self.items.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath as IndexPath], with: .fade)

    }

    var editAction = UITableViewRowAction(style: .normal, title: "Edit") { action in



    }

    return [deleteAction, editAction]
}

我没有在网上找到有关此的信息。提前致谢!

【问题讨论】:

  • 怎么知道你想删除哪个孩子?即我想删除一个值为 'pizza' 的孩子 'favorite_food:'。
  • 我想删除整个孩子。例如 -KWKZGKMTjwkyjpQtwPn 及其所有内容
  • 基于什么参数?是什么让那个子节点需要被删除?是因为它符合/不符合标准吗?因为它的价值是“披萨”?因为用户刷卡删除了?您需要考虑要删除节点的原因是什么。一旦我们知道这一点,我们就会有答案。
  • @Jay 他想删除它,因为用户删除了 tableView 中的项目。
  • @ChandlerLong 你在哪里用inventory 节点中的孩子填充你的tableView?

标签: swift firebase firebase-realtime-database ios10


【解决方案1】:

让我们从一个简化的结构开始

-Uiiasidasod
   item_name: "iMac"
-Yiimamsmdd
   item_name: "MacBook"

典型的设计模式是从 Firebase 读取这些项目并将它们存储在一个字典数组中。该数组用作 tableViews 的 数据源

每个字典都是一个键值对。

所以上面的第一个节点有一个 Firebase 键(父节点名称为“Uiiasidasod”,值为“item_name: iMac”(注意该值也是字典)

myArray[0] = ["Uiiasidasod": "item_name: 'iMac'"]
myArray[1] = ["Yiimamsmdd": "item_name: 'MacBook'"]

因此,当用户在表中滑动第 1 行时,您会从数组第 1 行中获取字典。您会获得关键部分(Firebase 节点名称),然后可以将其从 Firebase 中删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2017-12-05
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2021-12-25
    • 2016-11-09
    相关资源
    最近更新 更多