gpt4 book ai didi

swift - 在dispatch_get_global_queue完成后加载数据

转载 作者:行者123 更新时间:2023-11-30 13:37:44 24 4
gpt4 key购买 nike

好吧,我正在从互联网获取数据,该数据类似速度较慢

所以我必须将其加载到dispatch_get_global_queue中,但在viewDidLoad上

但主要问题是,如果有 collectionView 或表,它将在从互联网获取数据时加载,然后部分的 Collection View 给我 nil ,如何在dispatch_get_global_queue完成后加载 Collection View

import UIKit


class SectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet weak var sectionTab: UIImageView!
@IBOutlet weak var sectionTitle: UILabel!
var sectionsPosts:JSON!
@IBOutlet weak var collectionView: UICollectionView!
var sectionsTap:JSON!

override func viewDidLoad() {
super.viewDidLoad()

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {

if Reachability.isConnectedToNetwork() == true {
if let url = NSURL(string: "http://nepraswebsite.com/new_lille/mobile_api/blog/first_tab/1") {
if let data = try? NSData(contentsOfURL: url, options: []) {
self.sectionsTap = JSON(data: data)
}
}

if let newurl = NSURL(string: "http://nepraswebsite.com/new_lille/mobile_api/blog/posts_by_category/" + sections[tapIndex]["Slug"].stringValue + "/1") {
if let newdata = try? NSData(contentsOfURL: newurl, options: []) {
self.sectionsPosts = JSON(data: newdata)
}
}
self.collectionView.reloadData()

}

}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return sectionsPosts.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cellA = collectionView.dequeueReusableCellWithReuseIdentifier("postsCell", forIndexPath: indexPath) as! PostsCollectionViewCell


cellA.postImage.sd_setImageWithURL(NSURL(string: sectionsPosts[indexPath.row]["Image"].stringValue
), placeholderImage: UIImage(named: "demopic"))
cellA.postTitle.text = sectionsPosts[indexPath.row]["Title"].stringValue
return cellA

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

UIApplication.sharedApplication().openURL(NSURL(string: sectionsPosts[indexPath.row]["URL"].stringValue
)!)


}

}

最佳答案

您的numberOfItemsInSection需要知道后台任务尚未完成。一种方法是:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sectionPosts?.count ?? 0
// or
return sectionsPosts == nil ? 0 : sectionsPosts.count
}

加载完成后,您可以设置此变量并调用 reloadData 进行显示。

但是,仍然存在一些问题。首先,需要在主队列上调用 reloadData,如下所示:

dispatch_async(dispatch_get_main_queue(), {
self.collectionView.reloadData()
})

其次,Apple 建议使用 dataTaskWithURL:completionHandler:(在 NSURLSession 上)而不是 contentsOfURL:。这将为您在后台线程中运行任务,并为您的响应代码提供完成处理程序。您可以在 NSData 文档 heredataWithContentsOfURL:options:error: 部分找到此建议。 .

关于swift - 在dispatch_get_global_queue完成后加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35903867/

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