线程1 EXC_BAD_ACCESS(代码= 2,地址= 0x7ffeeb1aeff8)

时间:2018-11-27 11:22:31

标签: swift viper viper-architecture

我正在尝试学习VIPER。我遵循了本教程:VIPER Tutorial。我有一个交互者和演示者。

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

现在,这里发生了什么,我使路由器工作,视图即将到来,它正在调用presenter,presenter正在调用interactor,interactor成功调用了API并获取了数据,现在是时候返回从Interactor到Presenter的数据,并在此不断抛出此错误。而且,我不知道为什么有人可以提供帮助?

1 个答案:

答案 0 :(得分:0)

我认为您有一个循环调用,可能您的交互器未完全初始化,然后您要从中获取数据,然后出现“访问错误”。