首先有人可以帮我提出更好的头衔吗?我只是不知道这个的正确术语。
这是我的代码,
func loadPublicFeed() {
ref = FIRDatabase.database().reference()
ref.child("brackets").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
if let bracketsSnapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for brackets in bracketsSnapshot {
if let bracketsDict = brackets.value as? Dictionary <String, Any> {
let key = brackets.key as String
let post = BracketsPublicFeed(postKey:key, postData: bracketsDict)
self.posts.insert(post, at: 0)
}
}
}
self.stopRefresher()
self.collectionView.reloadData()
self.watchlistClicked = false
})
}
下载数据然后做东西。我想把这个函数重构它,所以我可以调用重构函数并添加这些
self.stopRefresher()
self.collectionView.reloadData()
self.watchlistClicked = false
进入它。
它可能看起来像这样,
func loadFeedTest() {
fetchTest.loadPublicFeed(collectionView: self.collectionView, completionHandler: { () -> Void in
self.stopRefresher()
self.collectionView.reloadData()
self.watchlistClicked = false
})
我尝试使用completionHandler
之类的func loadPublicFeed(completionHandler: () -> Void)
代码。我做了很多这方面的变化。我觉得我可能走在正确的道路上,但我无法确定这一点。
答案 0 :(得分:2)
您可以通过以下方式重构您的功能:
func loadPublicFeed(collectionView: UICollectionView, completionHandler:() -> Void) {
ref = FIRDatabase.database().reference()
ref.child("brackets").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
if let bracketsSnapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for brackets in bracketsSnapshot {
if let bracketsDict = brackets.value as? Dictionary <String, Any> {
let key = brackets.key as String
let post = BracketsPublicFeed(postKey:key, postData: bracketsDict)
self.posts.insert(post, at: 0)
}
}
}
completionHandler();
})
}