Swift Pulltorefresh on tableview 可能导致线程 1: EXC_BAD_ACCESS (code=1, address=0x53178d8ac) 来自提供的代码

问题描述

最近在我的主页视图控制器上测试我的拉动刷新功能时,在大约 10-30 次拉动刷新后,有时控制器会因线程 1 崩溃:EXC_BAD_ACCESS(代码 = 1,地址 = 0x53178d8ac)

这是我的下拉刷新功能中的代码

//this function is for the tableview controllers pull to refresh
@objc func refreshwaspulled(){
    
    //check if this is for the trending screen
    if isRepresentingExplorePageClick == true {
        
        dispatchQueue.main.async {
            self.refreshControl.endRefreshing()
        }
        
    }else if isRepresentingFollowingData == false {
        //call the API
        HomepageAPInetwork.userTrendingContent(accessKey: accessKey,completionHandler: { posts in
            //set the posts
            self.followingPosts.removeAll()
            self.followingPosts = posts
            
            //call the main thread
            dispatchQueue.main.async {
                
                //reset the estimatedrowheight
                self.Tableview.estimatedRowHeight = 600
                //set the rowheight to use the automatic dimension
                self.Tableview.rowHeight = UITableView.automaticDimension
                
                self.refreshControl.endRefreshing()
                
                //reload the data
                //begin tableview updates
                self.Tableview.reloadData()
            }
            
        })
        
        
    }else if isRepresentingFollowingData == true {
        
        HomepageAPInetwork.userFollowingContent(accessKey: accessKey,completionHandler: {posts in
            self.followingPosts.removeAll()
            //set the posts
            self.followingPosts = posts
            
            //call the main thread
            dispatchQueue.main.async {
                
                //reset the estimatedrowheight
                self.Tableview.estimatedRowHeight = 600
                //set the rowheight to use the automatic dimension
                self.Tableview.rowHeight = UITableView.automaticDimension
                
                //end the refresh control
                self.refreshControl.endRefreshing()
                

                //reload the data
                self.Tableview.reloadData()
            }
            
        })
        
    }

解决方法

因为我在主页上使用了自定义的 Scrolltorow 功能,所以这里实际上搞乱了刷新,这是我为解决这个问题所做的。

之前

self.refreshControl.endRefreshing()

之后

if self.refreshControl.isRefreshing == true {
   self.refreshControl.endRefreshing()
}