2015-05-08 44 views
0

我試圖從查詢結果傳遞到變量數組傳遞從findObjectsInBackgroundWithBlock結果到一個變量

var petitions = [PFObject] = [] 

然後返回結果。我如何能在Swift

func getPetitions(employeeId: String, employeeBusiness: String) -> [PFObject] { 

    var petitions: [PFObject] = [] 

    var query = PFQuery(className:"Petitions") 
    query.selectKeys(["petitionDate", "availableFrom", "availableTo"]) 
    query.whereKey("employeeId", equalTo:employeeId) 
    query.whereKey("employeeBusiness", equalTo:employeeBusiness) 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [AnyObject]?, error: NSError?) -> Void in 
     if error == nil { 
      if let objects = objects as? [PFObject] { 
       NSLog("Successfully retrieved \(objects.count) petitions.") 
       for object in objects { 
        petitions.append(object) 
       } 
      } 
     } 
    } 

    return petitions 
} 

回答

0

您誤解了完成塊。你不能從這樣的異步調用中返回結果。相反,您需要使該函數成爲void(無結果),然後將處理結果的代碼放入完成塊中。

您應該重構您的getPetitions方法,將它自己的完成塊作爲參數。一旦完成了異步Parse調用,並且您完成了追加項目的代碼,您就可以調用提供的塊。

+0

你能給我舉個例子嗎?提前致謝 – Xsan

1

查詢已同步通過,這意味着query.findObjectsInBackgroundWithBlock在您返回petitions時可能無法完成(99%無法完成)。

你可以做你想要直接在功能做你的東西:

var petitions: [PFObject] = [] //class variable 

func getPetitions(employeeId: String, employeeBusiness: String) { 

    var query = PFQuery(className:"Petitions") 
    query.selectKeys(["petitionDate", "availableFrom", "availableTo"]) 
    query.whereKey("employeeId", equalTo:employeeId) 
    query.whereKey("employeeBusiness", equalTo:employeeBusiness) 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [AnyObject]?, error: NSError?) -> Void in 
     if error == nil { 
      if let objects = objects as? [PFObject] { 
       NSLog("Successfully retrieved \(objects.count) petitions.") 
       for object in objects { 
        self.petitions.append(object) 
       } 
       // update UI 
       // or just do whatever you want with the petitions. 
      } 
     } 
    } 
} 

或者你可以寫一個塊功能:

func getPetitions(employeeId: String, employeeBusiness: String, block:PFArrayResultBlock) { 

     var query = PFQuery(className:"Petitions") 
     query.selectKeys(["petitionDate", "availableFrom", "availableTo"]) 
     query.whereKey("employeeId", equalTo:employeeId) 
     query.whereKey("employeeBusiness", equalTo:employeeBusiness) 
     query.findObjectsInBackgroundWithBlock(block) 
     } 
    } 

要叫它:

getPetitions("employeeId", "employeeBusiness", block: { 
    (objects: [AnyObject]?, error: NSError?) -> Void in 
      // do whatever you want with your objects.. 
      // update UI 
      // or just do whatever you want with the petitions. 
}) 

解決這個問題的另一種方法是同步查詢:

func getPetitions(employeeId: String, employeeBusiness: String) -> [PFObject] { 
    var query = PFQuery(className:"Petitions") 
    query.selectKeys(["petitionDate", "availableFrom", "availableTo"]) 
    query.whereKey("employeeId", equalTo:employeeId) 
    query.whereKey("employeeBusiness", equalTo:employeeBusiness) 
    var objects = query.findObjects() 
    if let petititons = objects as? [PFObject] { 
     return petititons 
    } else { 
     return [PFObject]() // return an empty pf object array. If you want, return nil.. but the return value must be [PFObject]? if you do this. 
    } 
} 
相關問題