gpt4 book ai didi

ios - SWIFT - 如何修复回调发生得太晚

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

我正在进行网络调用,取回数据并希望通过向 tableVC 中的 var 提供数据来将该数据用于 tableVC。我遇到的问题是我的回调返回数据太晚了。下面是我的代码。我真的很感激任何建议

来自网络管理员的代码

  func getArticles(completion: @escaping PListCompletion) {

guard let url = plistUrlString else { return }
let urlRequest = URLRequest(url: url)
let defaultConfig = URLSessionConfiguration.default
let session = URLSession(configuration: defaultConfig)
let task = session.dataTask(with: urlRequest) { (data, response, error) in
do {
if let data = data {
let articles = try PropertyListDecoder().decode([Articles].self, from: data)
self.callBackOnMainThread(completion: completion, response: .success(response: PlistResponseData(results: articles)))
}
} catch {
print(error)
completion(.failure(error: error))
}
}
task.resume()
}

private func callBackOnMainThread(completion: @escaping PListCompletion, response: PlistResponse) {
DispatchQueue.main.async {
completion(response)
}
}
final class ArticleListProvider {

var articles: [Articles]

convenience init() {
self.init(articles: [])

self.provideArticles { (article) in
self.articles.append(contentsOf: article)
}
}

init(articles: [Articles]) {
self.articles = articles
}

func articleCount() -> Int {
//used for tableVC cells but returning 0
return self.articles.count
}

func articleAtIndex(_ index: Int) -> Articles? {
return articles[index]
}

func provideArticles(completion: @escaping ([Articles]) -> Void) {
ArticleDetailsNetworkManager.sharedInstance.getArticles { [weak self] (response) in
guard let weakSelf = self else { return}
switch response {
case .success(let plistData):
weakSelf.articles = plistData.results
case .failure(let error):
print(error.localizedDescription)
}
completion(weakSelf.articles)
}
}
}

表VC

final class ArticleListController: UITableViewController {
fileprivate let CellIdentifier = "Cell"

fileprivate let articleListProvider: ArticleListProvider

override init(style: UITableViewStyle) {
self.articleListProvider = ArticleListProvider()
super.init(style:style)
}

required init?(coder: NSCoder) {
self.articleListProvider = ArticleListProvider()
super.init(coder: coder)
}

override func viewDidLoad() {
super.viewDidLoad()
setupTableViewCell()
tableView.reloadData()
}
}

extension ArticleListController {

func setupTableViewCell() {
tableView.register(ArticleTableViewCell.self, forCellReuseIdentifier: CellIdentifier)
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

//this is returning 0 when it should be returning the amount of articles from the articleProvider, where the networking call is being made

return articleListProvider.articleCount()
}

我不知道这是否重要,但这在 Xcode 9.4 上有效(尽管没有回调),升级到 10.1 后出现了这个问题

最佳答案

这是一种理想的行为,api 调用是异步的,它不会立即返回任何内容。因此,在该 Web 服务返回结果之前,数组计数将为零。

像这样改变你的初始化方法:

convenience init(callback: @escaping () -> Void) {
self.init(articles: [])

self.provideArticles { (article) in
self.articles.append(contentsOf: article)
callback()
}
}

并通过以下方式在您的 View Controller 中使用它:

self.articleListProvider = ArticleListProvider{ [weak self]
guard let weakSelf = self else { return}
weakSelf.yourTableView.reloadData()
}

关于ios - SWIFT - 如何修复回调发生得太晚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54683097/

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