Thread 1 EXC_BAD_ACCESS (code=2, address=0x7ffeeb1aeff8)

2.4k Views Asked by At

I am trying to learn VIPER. I followed this tutorial. I have these Interactor and Presenter:

class PPresenter: ViewToPresenterProtocol {
    
    var view: PresenterToViewProtocol?  
    
    var router: PresenterToRouterProtocol? = PRouter()
    
    var interactor: PresenterToInteractorProtocol? = PInteractor()

    
    func initiateFetch() {
        interactor?.fetchMatches()
    }
    
    func showMatchScreen(navigationC: UIViewController) {
        router?.pushToMatchDetailScreen(navigationC: navigationC)
    }
}

extension PPresenter: InteractorToPresenterProtocol {
    func matchFetched(match: MatchDetails?, banner: Banner?) {
        print(match!)
        
        print(banner!)
    }
    
    func matchFetchError() {
        //TODO
    }
}


class PInteractor: PresenterToInteractorProtocol {
    
    var presenter: InteractorToPresenterProtocol? = PPresenter()
    
    var live: Live?
    
    var upcoming: Upcoming?
    
    var banners: Banner?
    
    func fetchMatches() {
        let parameters = ["api_token" : Constants.USER_INFO["api_token"].rawValue,"player_id" : Constants.USER_INFO["player_id"].rawValue]
        
        ServiceHelper.sharedInstance.sendRequest(path: "get-predictor", params: parameters, showSpinner: true) { (response, error) in
            
            if let error = error {
                print("Unable to fetch match listing",error.localizedDescription)
                
                return
            } else {
                guard let obj = try? JSONDecoder().decode(MatchDetails.self, from: response.rawData()) else { self.presenter?.matchFetchError(); return }
                
                guard let bannerObj = try? JSONDecoder().decode(Banner.self,from: response.rawData()) else {self.presenter?.matchFetchError(); return }
                
                self.presenter?.matchFetched(match: obj, banner: bannerObj)
            }
        }
    }
}

Now, what is happening here, I get the router working, the view is coming, it is calling presenter, the presenter is calling the interactor, the interactor is successfully calling the API and getting the data and now it is time to return the data received from Interactor to Presenter and here it constantly throwing the following error:

Thread 1 EXC_BAD_ACCESS (code=2, address=0x7ffeeb1aeff8)

1

There are 1 best solutions below

1
On

I think you have a cyclic call, maybe your interactor is not fully initialized and then you want data from it and then you got "Bad access error".