gpt4 book ai didi

ios - 在 Swift 中解决异步关闭问题不起作用?

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:36 25 4
gpt4 key购买 nike

我已经把问题归结为这个

这个闭包:

  override func viewDidLoad() {
super.viewDidLoad()
let data = homeDataSource()
getPrivatePosts { (posts) in
print("postsCOUNT" , posts!.count)
data.posts = posts!
}
self.datasource = data
collectionView?.reloadData()

}

打印出“postCOUNT 1 postCOUNT 3”

然后当我打印 data.posts 的计数时,我得到 0...这是怎么回事?这是完整的代码

这是一个自定义的 UICollectionView:

import LBTAComponents
import Firebase
class homeView: DatasourceController {

override func viewDidLoad() {
super.viewDidLoad()
let data = homeDataSource()
getPrivatePosts { (posts) in
print("postsCOUNT" , posts!.count)
data.posts = posts!
}
self.datasource = data
collectionView?.reloadData()

}

override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width , height: 150)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 0 )
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 0)
}
// just to test
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToNewPost", sender: self)
}


func getPrivatePosts(completion : @escaping (_ privatePosts : [Post]?) ->()){
// fill posts array with posts from all buddys "privataPosts only"


var ret = [Post]()
staticValuesForData.instance.dataBaseUserref.child((Auth.auth().currentUser?.uid)!).child("contacts").observe( .value , with: { (snapshot) in

let dict = snapshot.children.allObjects as! [DataSnapshot]
for d in dict{
if let contactUid = d.childSnapshot(forPath: "uid").value as? String{


staticValuesForData.instance.dataBaseUserref.child(contactUid).child("privatePosts").observe( .value, with: { (snapshot) in

let posts = snapshot.children.allObjects as! [DataSnapshot]
print("postval" , posts)

for post in posts{

if let dict = post.value as? [String : AnyObject]{
let fullname = dict["fullname"] as! String
let picUrl = dict["picUrl"] as! String
let postContent = dict["postContent"] as! String
let time = dict["time"] as! Int
let uid = dict["uid"] as! String
let username = dict["username"] as! String

print("first name of person who did the post" , fullname)

let reposts = dict["reposts"] as! [String]

let downs = dict["downs"] as! [String]
// possible issue
var comments = [Comment]()

let commentArr = snapshot.childSnapshot(forPath: "comments").children.allObjects as! [DataSnapshot]

for c in commentArr{
if let dict = c.value as? [String : AnyObject]{

let cuid = dict["uid"] as! String
let ccommentText = dict["commentText"] as! String
let cpicUrl = dict["picUrl"] as! String
let cusername = dict["username"] as! String
let ctime = dict["time"] as! Int

let com = Comment(uid: cuid, commentText: ccommentText, time: ctime, picUrl: cpicUrl, username: cusername)

comments.append(com)

}

}

print("HERE : post content\(postContent) username : \(username) commentArr \(comments)")

let postToAdd = Post(postContent: postContent, picUrl: picUrl, userName: username, fullName: fullname, postID: uid, postTime: time, downs: downs, reposts: reposts, comments: comments)

print("LOOK AT MEE \(postToAdd.userName) is the username of the post object \(postToAdd.postContent) is the contetn")

ret.append(postToAdd)
print("RET" , ret)
}
}
completion(ret) // this is where the completion block should be called
})

}
}
})
}

这是一个数据源对象:

import LBTAComponents
class homeDataSource: Datasource {

var posts = [Post]()



override func numberOfItems(_ section: Int) -> Int {
print("COUNT " , posts.count)
return posts.count

}

override func headerClasses() -> [DatasourceCell.Type]? {
return [userHeader.self]
}

override func footerClasses() -> [DatasourceCell.Type]? {
return [userFooter.self]
}

override func cellClasses() -> [DatasourceCell.Type] {
return [userCell.self]
}

override func item(_ indexPath: IndexPath) -> Any? {
return posts[indexPath.item]
}

框架可以在这里使用:

pod 'LBTAComponents'

最佳答案

你有两次相同的基本误解。

在第二个代码部分中,您创建了最初为空的 ret 变量,然后触发了一些异步任务。但是,您在异步任务之外调用 completion(ret),因此它会在异步任务完成之前立即触发,从而返回您的初始空值。

第一个代码也会遇到同样的问题,因为您最初创建的 postArray 是空的,然后调用提供完成处理程序的 getPrivatePosts 函数,但该完成处理程序将在异步任务中调用,因此可能会有延迟,但您使用立即值,因此将返回空的初始值。

关于ios - 在 Swift 中解决异步关闭问题不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48234060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com