2016-08-30 5 views
2

현재 Firebase를 사용하여 사람들의 데이터와 그에 대한 세부 정보를 저장하고 있습니다. 내가있는 tableView를 채우는 데이터베이스에서 정보를 검색하는 방법입니다 : 내 사용자의TableView에서 Firebase 노드를 삭제하는 방법

let ref = FIRDatabase.database().reference().child("Users") 
    ref.observeEventType(.ChildAdded, withBlock: 
    { (snapshot) in 

     if let firstname = snapshot.value?.objectForKey("firstname"), lastname = snapshot.value?.objectForKey("lastname") 
     { 
      let fullname = "\(firstname) \(lastname)" 
      self.names.append(fullname) 
      self.tableView.reloadData() 
     } 

    }, withCancelBlock: nil) 

각각 고유 한 ID를 가지고 있으며, 그에 따라 삭제하려고 해요. 지금까지 난 단지 데이터베이스에서있는 tableView의 끝에서가 아니라 내 사용자를 삭제 : 내 observePeople() 함수에서

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if editingStyle == .Delete 
    { 
     names.removeAtIndex(indexPath.row) 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

     //Delete from firebase 
    } 
} 

을 아이가과 같이 제거하면 나는 관찰 할 수있는 코드를 구현 :

ref.observeEventType(.ChildRemoved, withBlock: 
    { (snapshot) in 

    if let firstname = snapshot.value?.objectForKey("firstname"), lastname = snapshot.value?.objectForKey("lastname"), dateOfBirth = snapshot.value?.objectForKey("Date of Birth"), zipcode = snapshot.value?.objectForKey("Zipcode") 
    { 
     print("We deleted the person \(firstname) \(lastname) with the details: \(dateOfBirth), \(zipcode)") 
    } 

    }, withCancelBlock: nil) 

그렇다면 누군가가 tableView에서 스 와이프하여 삭제할 때 ID를 기반으로 정확한 사용자를 삭제하려면 어떻게해야합니까?

답변

1

사용자를 삭제하려는 참조에서 removeValue() 메서드를 사용할 수 있습니다. 당신이 //Delete from firebase을 쓴 당신의 commitEditingStyle 테이블 뷰 방법에 따라서

당신이 뭔가를 삽입 할 수있는 곳입니다 :

ref.child("Users/\(uniqueUserID)").removeValue() 

당신은 당신이 당신의 테이블에서 삭제 indexPath.row에서 사용자의 uniqueUserID을 얻을 필요가있을 것이다 전망.

JSON 트리가 사용자에게 어떻게 보이는지 예제를 입력하면 더 자세히 설명 할 수 있습니다.

이와 비슷한가요?

"root" 
    "Users" 
     "uniqueUserID" 
      "firstname" 
      "lastname" 
      "dateOfBirth" 
      "zipcode" 

편집 당신의 사전 나는 이런 식으로 뭔가 할 것 이상으로 무엇을 보이는 경우

ref.observeEventType(.ChildAdded에서 : 당신이 //Delete from firebase을 쓴 commitEditingStyle 테이블 뷰 방법에서

//names will be the array that stores the user data that you retrieve from Firebase 
//names will be an array of dictionaries 
//each dictionary will represent a user object that includes firstname, lastname, AND uniqueUserID 
self.names = [[String : AnyObject]]()//make a new clean array 
//create a dictionary to store the user data 
let user = snapshot.value as! [String: AnyObject] 
//get the uniqueUserID which is the `snapshot.key` 
let uniqueUserID = snapshot.key as! String 
//add the uniqueUserID to the user dictionary 
user["uniqueUserID"] = uniqueUserID as! String 
self.name.append(user) 
self.tableView.reloadData() 

를 :

//get the user at the `index.row` in the names array 
let user: [String: AnyObject] = self.names[indexPath.row] 
let uniqueUserID = user["uniqueUserID"] 
//get the uniqueUserID 
let ref = FIRDatabase.database().reference() 
//remove that user at that uniqueUserID 
ref.child("Users/\(uniqueUserID)").removeValue() 
+0

그렇습니다. 정확히 나무 모양입니다. –

0

전체 이름을 일치시켜 사용자의 고유 ID 인 스냅 샷의 키를 찾을 수있었습니다. 그러나 항목을 삭제 한 후에 하나의 항목 만 남을 때까지 색인을 삭제하고 "범위를 벗어난 색인"오류가 발생하는 오류가 발생했습니다.

let ref = FIRDatabase.database().reference().child("Users") 
     ref.observeEventType(.ChildAdded, withBlock: { (snapshot) in 

      if let firstname = snapshot.value?.objectForKey("firstname"), lastname = snapshot.value?.objectForKey("lastname") 
      { 
       let fullname = "\(firstname) \(lastname)" 
       let currentName = self.names[indexPath.row] 

       if fullname == currentName 
       { 
        print("We have a match") 
        let currentKey = snapshot.key 
        ref.child(currentKey).removeValue() 

        dispatch_async(dispatch_get_main_queue()) 
        { 
         self.tableView.reloadData() 
        } 
       } 

      } 

     }, withCancelBlock: nil) 
관련 문제