2017-04-05 65 views
0

我想填充數組postCaptions。 我需要將此數組返回給我的表視圖的numberOfRowsInSection,但它每次都返回一個空數組。數組返回PFQuery之外的空數組與解析

var postCaptions = [String]() 

    query2.whereKey("userid", equalTo: PFUser.current()?.objectId!)       
    query2.findObjectsInBackground(block: { (objects, error) in 
     if let userPosted = objects {  
      for object in userPosted {   
       if let userPost = object as? PFObject {   
        self.postImageFiles.append(userPost["userPostImage"] as! PFFile)          
        self.postLocation.append(userPost["userPostLocation"] as! String) 
        self.postCaptions.append(userPost["postCaption"] as! String) 
        print(self.postCaptions.count) //returns value of 2 for the two posts that I've made     
        self.tableView.reloadData()    
        self.refresher.endRefreshing() 
       } 
      } 
     } 

    })   
    print(self.postCaptions.count) //Returns empty array 

我知道問題出在線程的順序,我明白,但我不知道我能做些什麼使陣列保持查詢之外填充。我已經將此視爲解決此問題的一種方法,但我真的沒有找到直接的解決方法,可以在代碼中解決這個問題。如果有人能提供我的代碼問題的解答,這將是驚人的!:)我一直堅持這個問題的一個多星期,現在

var postCaptions = [String]() { 
didSet { 
    // Do any execution that needs to wait for postCaptions here. 
} 
} 

** cellForRowAt

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "userPhotoFeedCell", for: indexPath) as! FeedCell 

    if PFUser.current()?.objectId != nil { 

     cell.userUsername.text = PFUser.current()?.username 

    } 


    //downloads images for user profile pic 
    imageFiles[indexPath.row].getDataInBackground { (data, error) in 

     if let imageData = data { 

      let downloadedImage = UIImage(data: imageData) 

      cell.userProfilePic.image = downloadedImage 

     } 


    } 

    //downloades images and items to populate user post 
    postImageFiles[indexPath.row].getDataInBackground { (data, error) in 

     if let userPostImageData = data { 

      let downloadedUserPostImage = UIImage(data: userPostImageData) 

      cell.userFeedPhoto.image = downloadedUserPostImage 

     } 


    } 

    cell.postLocation.text = postLocation[indexPath.row] 

    cell.postCaption.text = postCaptions[indexPath.row] 

    cell.userFeedPhoto.image = UIImage(named: "OrangeFuego") 

    cell.userProfilePic.image = UIImage(named: "usericon.png") 

    return cell 

} 
+0

'findObjectsInBackground'以異步方式工作。打印行在數據返回之前執行。將打印線放入完成塊。 – vadian

+0

是的,但我需要延遲numberOfRowsInSection的調用,直到完成塊完成加載,並因此給出了postCaptions的值 –

+0

'numberOfRowsInSection'從框架中調用。填充數據源數組並調用'reloadData()'。 – vadian

回答

0

如果我理解你的問題很簡單。將var postCaptions = [String]()設置爲所需類別中的全局變量。然後將您的值從迭代中附加到您完成的方式。然後,可以在代碼的其他部分訪問postCaptions值,並且可以使用count屬性。

+0

變量不是問題,問題在於findObjectsInBackground在調用numberOfRowsInSection之前永遠不會完成,所以數組始終爲0 –

0

由於方法findObjectsInBackground暗示它在後臺執行,這意味着它與您的UI無關,因此在完成此功能之前,您的數組將保持爲空。在打印該數組時,此功能尚未完成,因此postCaptions現在將爲空。

而且這就是爲什麼numberOfRows將爲零。

我認爲您需要做的是將此查詢放入viewDidLoad或您指定的初始化程序中,然後像在完成程序段中一樣調用tableView.reloadData()。此外,您可能需要聲明Bool,以確定表是否正在加載,並且在完成塊中,成功執行查詢後將isLoading設置爲false。

query2.findObjectsInBackground(block: { (objects, error) in 

    if let userPosted = objects { 
     self.isLoading = false 

     for object in userPosted { 

      if let userPost = object as? PFObject { 

       self.postImageFiles.append(userPost["userPostImage"] as! PFFile) 

       self.postLocation.append(userPost["userPostLocation"] as! String) 

       self.postCaptions.append(userPost["postCaption"] as! String) 

       print(self.postCaptions.count) //returns value of 2 for the two posts that I've made 

      } 

     } 

     self.tableView.reloadData() 

     self.refresher.endRefreshing() 

    } 

}) 

和裏面的numberOfRowsInSection

if isLoading { //you need to declare isLoading 
    return 1 //maybe a dummy cell to tell user the table is loading 
} else { 
    return postCaptions.count 
} 
+0

我喜歡這裏要去的地方,但是我試過了,isLoading永遠不會等於false,因爲在調用NumberOfRowsInSection時,findObjectsInBackground始終會繼續加載。我需要一些方法來等待findObjectsInBackground完成,然後再調用其餘的方法。 –

+0

@FinnWolff也許你應該在你的'for-in'循環外加'reloadData()'和'self.isLoading = false'。看到我上面編輯的答案。由於在查詢(後臺線程)完成執行後你已經調用了'reloadData()',因此理論上,設置後'isLoading'應該是false。因爲每次調用reloadData()時,都會調用'numberOfRows'方法。 –

+0

因此,我將print(「IT WORKED」)添加到了numberOfRowsInSection的if isLoading = false return postCaptions.count部分。它確實工作!該數組是填充和一切,但是當我滾動應用程序中的飼料崩潰,說:索引超出範圍 –