How to check for value in Firebase that is held under a autoID child?

微笑、不失礼 提交于 2019-12-20 07:26:59

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/40876616/how-to-check-for-value-in-firebase-that-is-held-under-a-autoid-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!