2016-10-15 5 views
1

개체가 현재 사용자에게 속하고 해당 requestResponded가 true가 아닌 경우에만 swift를 통해 UserRequests 클래스에서 개체를 삭제하려고합니다. 그러나 objects.deleteInBackground() 때 오류가 발생하고이 줄을 제거 할 때 함수가 여전히 작동하지 않습니다.PFQuery를 통해 구문 분석에서 개체를 삭제하는 방법

func deleteRequest(){ 
    let check = PFQuery(className: "UserRequests") 
    check.whereKey("requestResponded", equalTo: "True") 

    let query = PFQuery(className: "UserRequests") 
    query.whereKey("username", equalTo: (PFUser.currentUser()?.objectForKey("username") as! String)) 
    query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 
     if objects != nil && error == nil{ 
      // Successfully retrieved the object 
      check.getFirstObjectInBackgroundWithBlock { 
       (object: PFObject?, error: NSError?) -> Void in 
       if error != nil || object == nil { 
        print("Not accepted.") 
        object!.deleteInBackground() 
        objects.deleteInBackground() 
       } else { 
        print("Successfully retrieved the object.") 
       } 
      } 
     }else{ 
      self.performSegueWithIdentifier("requestAccepted", sender: self) 
     } 
    }) 
} 

답변

1

왜냐하면 objects는 객체의 목록이기 때문입니다. 당신은 예를 들어 1

에 의해 객체 하나를 삭제해야합니다 또한

for object in objects { 
    object.deleteInBackground() 
} 

을, 두 개의 질의가 동일한 클래스에 속해 있기 때문이다. 나는 1 개 쿼리

UPDATE를 사용하는 것이 좋습니다 것

func deleteRequest(){ 
    let query = PFQuery(className: "UserRequests") 
    // the key "requestResponded" is not True 
    query.whereKey("requestResponded", equalTo: "False") 
    // for deleting the object is that it belongs to the current user 
    query.whereKey("username", equalTo (PFUser.currentUser()?.objectForKey("username") as! String)) 
    query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 
     if error != nil{ 
      print(error) 
     } 
     // objects are those the key "requestResponded" is not True and belongs to the current user 
     for object in objects { 
      object.deleteInBackground() 
     } 
     // other case 
     if objects.count == 0 { // no match result found 
     } 
    }) 
} 

당신은 여전히 ​​당신을 감사 SEGUE에게

+0

을 수행 할 때의 상태를 놓칠 것 같아요. 이 질문에 대해 어떻게 하나의 쿼리를 사용할 수 있습니까? –

+0

먼저, 개체를 삭제하기위한 조건과 같은 조건을 생각해 볼 필요가 있습니까? segue를 수행하기위한 조건은 무엇입니까? 그 사건은 어때? –

+0

좋아. 질문에서 언급했듯이 객체를 삭제하기위한 조건은 현재 사용자에게 속하고 "requestResponded"키가 True가 아니라는 것입니다. –

관련 문제