【问题标题】:Cannot reload tableview within the amplify.api.query function无法在 amplify.api.query 函数中重新加载 tableview
【发布时间】:2021-04-19 07:24:47
【问题描述】:

我正在使用 Amplify 将数据加载到我的应用中。当我加载分页列表时,我将存储列表中的元素附加到我的数据模型数组中。但是,由于元素的加载需要一些时间,我要么必须在完成处理程序中的 amplify.api.query 函数的末尾调用 tableView.reloadData(),要么在我在 @ 中调用 events.append() 之后直接调用 tableView.reloadData() 987654325@ 功能。但是,amplify.api.query 既没有完成处理程序,也不允许我将 tableView.reloadData() 放在函数本身中,因为每次我尝试运行应用程序时,都会弹出一个错误,提示“必须使用 UITableView.reloadData()仅限主线程。”我希望在UIViewController 的启动时加载表格视图。我该怎么做?

func listFirstPage(finished: () -> Void) {

        Amplify.API.query(request: .paginatedList(Items.self, where: nil, limit: 1000)) { item in
            switch item {
            case .success(let result):
                switch result {
                case .success(let items):
                   
                    self.currentPage = items
                    self.events.append(contentsOf: items)
                    print(self.items)
                    self.AddItemTableView.reloadData()
                    
                case .failure(let error):
                    print("Got failed result with \(error.errorDescription)")
                }
            case .failure(let error):
                print("Got failed event with error \(error)")
            }
        }
        
          
    }

【问题讨论】:

  • 不要问 tableView.reloadData 能为你做什么。询问您可以为异步数据做什么。

标签: ios swift aws-amplify


【解决方案1】:

DispatchQueue.main.async {
   self.AddItemTableView.reloadData()
}

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多