2017-12-25 1 views
1

나는 많은 것을 시도해 왔으며 인터넷에서 많은 것을 검색해 왔지만 코드가 어떻게 설정 되었기 때문에 나를 돕는 해결책을 찾을 수 없습니다.firebase에서 게시물을 삭제하는 방법은 무엇입니까? Swift

내가 게시물을 삭제하려고 한 방법 중 하나를하지만 난 정말 내 게시물 중포 기지에 의해 생성 된 자동 ID에 업로드되어 있기 때문에이 작업을 수행하는 방법을 모르는 내가

Database.database().reference.child("posts").child("HERE IS THE AUTO ID").removeValue 
여기에 작성하는 것을 잘 모릅니다

어떻게받을 수 있습니까? 제발이 문제에 대해 지금 당황했습니다. 자위를 얻는 방법에 대한 단서가 없습니다.

이 내가

if (self.imageFileName != "") { 
     if choosenCountryLabel.text == "Albania" { 
      // image has finshed the uploading, Saving Post!!! 
      if let uid = Auth.auth().currentUser?.uid { 

       Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in 
        if let userDictionary = snapshot.value as? [String: AnyObject] { 
         for user in userDictionary{ 
          if let username = user.value as? String { 
           if let streetAdress = self.locationAdressTextField.text { 
            if let title = self.titleTextField.text { 
             if let content = self.contentTextView.text { 
              let postObject: Dictionary<String, Any> = [ 
               "uid" : uid, 
               "title" : title, 
               "content" : content, 
               "username" : username, 
               "time" : self.timeStamps, 
               "timeorder" : self.secondTimeStamps, 
               "image" : self.imageFileName, 
               "adress" : streetAdress, 
               "postAutoID" : self.postAutoID 
              ] 


              let postID = Database.database().reference().child("posts").childByAutoId() 
              let postID2 = Database.database().reference().child("AlbaniaPosts").childByAutoId() 
              let postID3 = Database.database().reference().child(uid).childByAutoId() 

              postID.setValue(postObject) 
              postID2.setValue(postObject) 
              postID3.setValue(postObject) 
              let postAutoID = postID.key 
              let postAutoID2 = postID2.key 
              let postAutoID3 = postID3.key 
              print(postAutoID) 
              print(postAutoID2) 
              print(postAutoID3) 

              let alertPosting = UIAlertController(title: "Successfull upload", message: "Your acty was successfully uploaded.", preferredStyle: .alert) 
              alertPosting.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in 
               let vc = self.storyboard?.instantiateViewController(withIdentifier: "AlbaniaVC") 
               self.present(vc!, animated: true, completion: nil) 
              })) 
              self.present(alertPosting, animated: true, completion: nil) 



              print("Posted Succesfully to Firebase, Saving Post!!!") 

             } 
            } 
           } 
          } 
         } 
        } 
       }) 
      } 

     } 
    }else{ 
     let alertNotPosting = UIAlertController(title: "Seems like you got connection problems", message: "Your image has not been uploaded. Please Wait 10 seconds and try again.", preferredStyle: .alert) 
     alertNotPosting.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 
     self.present(alertNotPosting, animated: true, completion: nil) 
    } 

and here is the query in firebase.

+0

빠른 질문 : 사용자가 어떤 게시물을 어떻게 알 수 있습니까? 삭제 중입니까? –

+0

@ RosárioPereiraFernandes 오른쪽에서 왼쪽으로 스 와이프하여 특정 게시물을 삭제할 수 있습니다. – Jiyar

+0

"스 와이프를 삭제 하시겠습니까?"라는 코드를 게시 할 수 있습니까? 거기에서 당신을 도우려는 것이 더 쉬울 수도 있습니다. –

답변

0

Delete data

데이터를 삭제하는 가장 간단한 방법은 의 위치에 대한 참조에) (제거 호출하는 것입니다 게시물을 업로드하는 방법입니다 그 데이터.

또한 set() 또는 update()와 같은 다른 쓰기 작업의 값으로 null을 지정하여 삭제할 수도 있습니다. 이 기술을 update()와 함께 사용하면 단일 API 호출에서 여러 자식을 삭제할 수 있습니다.

+0

예 팁 주셔서 감사하지만 잘 모릅니다. 특정 행 progrommaticlly에 대한 자동 반복을 얻을 수있는 방법 – Jiyar

+0

알고 계십니까? 게시물을 통해 도달 한 다음 자동 생성 된 ID? – Jiyar

+0

HTML의 키 이름을 ['dataset' 속성]으로 지정합니다 (https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/). Use_data_attributes)를 삭제할 수있는 모든 요소에 적용됩니다. –

0

데이터를 쿼리하거나 업로드 할 때 게시물에 키를 저장해야 할 때 autoID를 저장해야합니다.

쿼리 할 때 키를 가져 오려는 경우. 다음과 같이 할 수 있습니다.

let ref = Database.database().reference() 
ref.child("posts").queryLimited(toLast: 7).observeSingleEvent(of: .value, with: { snap in 
    for child in snap.children { 
     let child = child as? DataSnapshot 
     if let key = child?.key { // save this value in your post object 
      if let post = child?.value as? [String: AnyObject] { 
       if let adress = post["adress"] as? String, let title = post["title"] as? String { // add the rest of your data 
        // create an object and store in your data array 
       }    
      } 
     } 
    } 
}) 

위 쿼리는 마지막 7 개의 게시물 만 가져옵니다. 계속 더 받으려면 pagination을 조사해야합니다.

당신은 당신이 이런 식으로 업로드 할 때 그냥 추가 게시물의 ID를 저장하려면 : 다음

let key = ref.child("posts").childByAutoId().key 

let post = ["adress": adress, 
      "content": content, 
      "postID": key] as [String: Any] 

let postFeed = ["\(key)" : feed] 

ref.child("posts").updateChildValues(postFeed, withCompletionBlock: { (error, success) in 
    if error != nil { 
     // report the error 
    } 
    else { 
     // everything is fine 
    } 
}) 

을 당신이 이런 일을 할 수 쿼리 할 때 : 지금

let ref = Database.database().reference() 
ref.child("posts").observeSingleEvent(of: .value, with: { snap in 
    for child in snap.children { 
     if let post = child?.value as? [String: AnyObject] { 
      if let postID = post["postID"] as? String { 
       // save this key in a post object so you can access it later to delete 
      } 
     } 
    } 
}) 

을 게시물이라는 객체를 만든 경우 해당 게시물을 삭제할 수 있습니다.

ref.child("posts").child(post.postID).removeValue(completionBlock: { (error, refer) in 
    if error != nil { 
     // failed to delete post      
    } 
    else { 
     // delete worked 
    } 
}) 
관련 문제