简体   繁体   English

无法在 amplify.api.query function 中重新加载 tableview

[英]Cannot reload tableview within the amplify.api.query function

I am using Amplify to load data to my app.我正在使用 Amplify 将数据加载到我的应用程序中。 When I load a paginated list, I'm appending elements from a stored list onto my data model array.当我加载分页列表时,我将存储列表中的元素附加到我的数据 model 数组中。 However, as the loading of the elements takes a bit of time, I either have to call tableView.reloadData() at the end of the amplify.api.query function in a completion handler or call tableView.reloadData() directly after I call events.append() within the amplify.api.query function.但是,由于元素的加载需要一些时间,我要么必须在amplify.api.query function 的末尾调用tableView.reloadData() ,要么在我调用后直接调用tableView.reloadData() amplify.api.query function 中的amplify.api.query events.append() However, amplify.api.query neither has a completion handler nor lets me put tableView.reloadData() within the function itself, as every time I try to and I run the app, an error pops up saying that "UITableView.reloadData() must be used from main thread only."但是, amplify.api.query既没有完成处理程序,也不允许我将tableView.reloadData()放在 function 本身中,因为每次我尝试运行应用程序时,都会弹出一个错误说“UITableView.reloadData()只能在主线程中使用。” I want the tableview to load upon the UIViewController 's startup.我希望在UIViewController启动时加载表格视图。 How should I do this?我该怎么做?

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)")
            }
        }
        
          
    }

Do

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM