配列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
}
'非同期で動作しますfindObjectsInBackground'。印刷行は、データが返される前に実行されます。印刷行を完成ブロックに入れます。 – vadian
はい、完了ブロックが読み込まれるまでnumberOfRowsInSectionの呼び出しを遅らせる必要があります。したがって、postCaptionsに値 –
を与えました。 'numberOfRowsInSection'がフレームワークから呼び出されました。あなたのデータソース配列にデータを入れ、 'reloadData()'を呼び出します。 – vadian