如何检查自动ID子项下的Firebase中的值? [英] How to check for value in Firebase that is held under a autoID child?

查看:247
本文介绍了如何检查自动ID子项下的Firebase中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



Firebase数据库





DataServices文件

  import Foundation 
导入Firebase
导入UIKit

让DB_BASE = FIRDatabase.database()。reference()。child(laboratory)//包含数据库的根
让STORAGE_BASE = FIRStorage.storage ().reference()
$ b $ class DataService {

static let ds = DataService()

//数据库引用
private var _REF_BASE = DB_BASE
private var _REF_STATION = DB_BASE.child(stations)
private var _REF_USERS = DB_BASE.child(users)


//存储引用
private var _REF_ITEM_IMAGE = STORAGE_BASE.child(item-pics)

var REF_BASE:FIRDatabaseReference {

返回_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


$ b $ //在firebase数据库中创建一个新用户

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

//从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 ]
}

我在网上找不到有关这方面的资讯。在此先感谢!

解决方案

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

  -Uiiasidasod 
item_name:iMac
-Yiimamsmdd
item_name:MacBook

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

每个字典都是一个key:value对。


因此,上面的第一个节点有一个Firebase关键字(父节点名称为Uiiasidasod,值为item_name:iMac(注意该值也是一个字典)。

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

所以当用户在表中的第1行滑动时,您将获得关键部分(Firebase节点名称),然后可以从Firebase中将其删除。


Upon the tap of a delete button. I am trying to delete a particular child out of my Firebase database. My problem is that I am unable to refer to the particular childByAutoId in my database therefore my application does not know what child to remove.

Firebase Database

DataServices File

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)
}

}

The information that I am trying to Delete is out of a tableViewCell. I have tried appending out the keys to an array but I am still unable to access the appropriate child to delete.

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]
}

I have found no information online about this. Thanks in advance!

解决方案

Let's start with a simplified structure

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

A typical design pattern is to read these items from Firebase and store them in an array of dictionaries. That array is used as a datasource for the tableViews.

each dictionary is a key:value pair.

So the first node from above has a Firebase key (parent node name of "Uiiasidasod" and a value of "item_name: iMac" (note the value is a dictionary as well)

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

so when the user swipes row 1 in the table, you fetch the dictionary from the array, row 1. You get the key portion (the Firebase node name) and can then delete it from Firebase.

这篇关于如何检查自动ID子项下的Firebase中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆