2015-05-01 44 views
0

我不知道如何獲得Parse.com的Top-50查詢, 那麼你能告訴我嗎?如何使用Parse.com獲得Top-50查詢

此代碼獲取所有查詢,但我想獲得前50名查詢。 但我不知道如何獲得前50名或最新50名的查詢。

func loadData() { 

    comments.removeAllObjects() 
    var query:PFQuery = PFQuery(className: "Comment") 
    query.orderByDescending("createdAt") 
    query.limit = 1000 
    query.findObjectsInBackgroundWithBlock{(objects: [AnyObject]!, error: NSError!) -> Void in 
     if (error != nil){ 
      //error 
     } 


     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 
      // Task 
      for object in objects { 

       if object["commentId"] as? String == nil { 
       self.comments.addObject(object) 
        if self.comments.count == 50 { 
         break 
        } 
       } 
      } 
      dispatch_async(dispatch_get_main_queue()) { 
       // UI 
       self.tableView.reloadData() 
       self.stopIndicator() 
      } 
     } 
    } 

} 

語言是Swift。

請回答我。

+0

@picot - 在解析,如果你不設置限制其默認給出的第100名對象在對象可用,這裏沒有必要檢查限制 –

+0

如果您的幫助,請參閱此[鏈接](HTTPS ://parse.com/questions/fetch-all-data-in-a-table-using-pfquery) –

+0

感謝您的評論,但我知道這一點。我想知道如何獲得最新的-50查詢。 – Picolt

回答

0

要查詢Parse上的最新50條記錄,您需要限制查詢50個對象(默認限制爲100)。

func loadData() { 

    self.comments.removeAllObjects() 
    var query:PFQuery = PFQuery(className: "Comment") 
    query.orderByDescending("createdAt") 
    query.limit = 50 //<--- change this line 
    query.findObjectsInBackgroundWithBlock{(objects: [AnyObject]!, error: NSError!) -> Void in 
      if (error != nil){ 
       //error 
      } 

      // Add the received objects to your array 
      self.comments.addObjectsFromArray(objects) 

      // No need to perform any task in background thread, so update the UI 
      self.tableView.reloadData() 
      self.stopIndicator() 
    } 
}