UIApplication applicationState只能从主线程使用

时间:2018-05-29 04:28:33

标签: ios swift uikit uiapplication

我正在尝试为API进行一些单元测试。这是实际的功能。

 func registerApi(path: String, player_id: Int, contest_id: Int, country_id: Int,  success: () -> Void)
{
    ServiceHelper.sharedInstance.sendRequest(path: "register-country",
                                             params: ["api_token": Constants.USER_INFO["api_token"].rawValue,
                                                      "player_id": player_id,
                                                      "country_id": country_id,
                                                      "contest_id": contest_id],
                                             showSpinner: true,
                                             completionHandler:
        { (response, error) in

            if (error != nil)
            {
             Test to be failed
            }
            else
            {
                Test Passed

            }
    })
}

而且,现在这里是单元测试的测试功能。

func testApiWorking()
{
    let controller = WorldCupChooseCountryVC()

    let expected = XCTestExpectation(description: "Some Countries to return")


    controller.registerApi(path: "get-country", player_id: 163, contest_id: 1, country_id: 1) { success in if success { expected.fulfill() }
    else{
        XCTFail()
        }
    }

    waitForExpectations(timeout: 1.0) { (_) -> Void in
    }

}

但是,每当我尝试测试时,我都会收到以下错误。

[UIApplication applicationState] must be used from main thread only

曾经,它运行良好,测试失败,但它运行了。但是,现在它还没有运行。

2 个答案:

答案 0 :(得分:1)

Main Thread Checker检测到来自后台线程的AppKit,UIKit和其他API的无效使用情况。 completionHandler似乎更新了UI,因此可能将该部分移动到单独的方法并使用Dispatch Queue调用它。

DispatchQueue.main.async {
     handleResponseOrError(response, error)
}

答案 1 :(得分:1)

  

(问题从关于主线程变为如何定义和调用闭包。这个答案已经改变以反映出来。)

     

[UIApplication applicationState]最有可能被调用   在GeneralHelper

内      

服务助手最有可能在a上发出网络请求   背景线程。

     

因此,为了确保在主线程上调用GeneralHelper,请执行   它通过DispatchQueue

(另外!你没有打电话给你的完成处理程序,所以我们也会添加它。)

func registerApi(path: String, player_id: Int, contest_id: Int, country_id: Int,  completion: ((Bool) -> Void)? = nil)
{
    let helper = ServiceHelper.sharedInstance
    let params = [
        "api_token": Constants.USER_INFO["api_token"].rawValue,
        "player_id": player_id,
        "country_id": country_id,
        "contest_id": contest_id
    ]
    helper.sendRequest(path: "register-country", params: params, showSpinner: true) { (response, error) in
        var success = true

        if let error = error
        {
            // Do stuff on failure.
            success = false
        }
        else
        {
            // Do stuff on success
        }

        // Call completion handler from 'registerApi'
        completion?(success)
    })
}

在测试中,您现在可以添加一个参数来测试是否成功。

func testApiWorking()
{
    let controller = WorldCupChooseCountryVC()

    let expected = XCTestExpectation(description: "Some Countries to return")

    controller.registerApi(path: "register-country", player_id: 163, contest_id: 1, country_id: 1) { success in
        if success {
            // Test success
        } else {
            // Test fail
        }

        expected.fulfill()
    }

    waitForExpectations(timeout: 30) { (_) -> Void in
    }
}
相关问题