swift - 如何克服导航到其他视图控制器 iOS swift 的延迟?
问题描述
我最近停止使用Alamofire
HTTP 网络调用,因为现在在 swift 4 Apple 已经引入了Codable
-protocol,它与 .protocol 相比非常强大和快速Alamofire
。一切正常,即使我的反应比以前更快。但是有一个问题。在使用Codable
协议解析数据之前,我导航到其他视图的速度非常快。但是现在我的导航有一点延迟,特别是在导航到我JSONDecoder
用来获得响应的页面时。例如:我有 VC1 和 VC2,VC1 是一个静态页面,我在 VC2 中使用JSONDecoder
并获取一些数据并将其显示在collectionView
. 现在我单击 VC1 中的一个按钮,它会在转到 VC2 之前停止一秒钟。我不知道为什么会这样。
VC1中的代码:
@IBAction func gotoProductCollectionView(_ sender: UIButton) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "productCollection") as! ProductCollectionViewController
self.navigationController?.pushViewController(viewController, animated: true)
}
VC2中的代码:
class ProductCollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var productsTableView: UITableView!
var productsData = [ProductData]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
getProductData()
}
func getProductData() {
self.productsData = []
guard let productDataURL = URL(string: "MyURL") else {return}
DispatchQueue.global(qos: .background).async {
do {
let data = try Data(contentsOf: productDataURL)
self.productsData = try
JSONDecoder().decode([ProductData].self, from: data)
print(self.productsData)
print(self.productsData.count)
for productNames in self.productsData {
print(productNames.product_name!)
}
}catch let error {
print(error)
}
DispatchQueue.main.async {
self.productsTableView.reloadData()
}
}
}
}
产品模型对象:
struct ProductData: Codable {
var product_id: String?
var product_name: String?
var product_price: String?
var product_special_price: String?
var product_sale_of: String?
var product_brand: String?
var product_image: String?
private enum CodingKeys: String, CodingKey {
case product_id
case product_name
case product_price
case product_special_price
case product_sale_of
case product_brand
case product_image
}
init(product_id: String? = nil, product_name: String? = nil, product_price: String? = nil, product_special_price: String? = nil, product_sale_of: String? = nil, product_brand: String? = nil, product_image: String? = nil) {
self.product_id = product_id
self.product_name = product_name
self.product_price = product_price
self.product_special_price = product_special_price
self.product_sale_of = product_sale_of
self.product_brand = product_brand
self.product_image = product_image
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
product_id = try container.decode(String.self, forKey: .product_id)
product_name = try container.decode(String.self, forKey: .product_name)
product_price = try container.decode(String.self, forKey: .product_price)
product_special_price = try container.decode(String.self, forKey: .product_special_price)
product_brand = try container.decode(String.self, forKey: .product_brand)
product_image = try container.decode(String.self, forKey: .product_image)
if let value = try? container.decode(Int.self, forKey: .product_sale_of) {
product_sale_of = String(value)
} else {
product_sale_of = try container.decode(String.self, forKey: .product_sale_of)
}
}
}
解决方案
答案就在这一行
let data = try Data(contentsOf: productDataURL)
显然,您从 Internet 下载数据DispatchQueue.main
您需要在单独的后台队列中执行此操作,然后main
再次更新视图。不要忘记使用.async
通话。
推荐阅读
- swift - 如何在 SwiftUI 中禁用子视图上的特定状态更改动画
- c# - 如果乘法结果值太大,则 C# 函数返回 int.MinValue
- android-security - 无法追踪意图重定向漏洞问题的根源
- python - 在 Python 中将输入从 float 向上舍入或向下舍入到 int
- java - Java/Spring 启动 - POST 请求不起作用后重定向
- c# - 我们可以在带有 MongoDB c# 驱动程序的投影中使用多个嵌套级别吗?
- python - 添加if语句时python循环中的倒数函数
- mysql - MySQL如何在字符串中查找url并从中删除一个字符?
- python - 如何用整数系数进行多项式除法
- html - 在同一 div 中的多个三角形中完美居中文本(响应式)