API调用会阻止UI线程Swift [英] API calls blocks UI thread Swift

查看:70
本文介绍了API调用会阻止UI线程Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要同步我的coredata中的Web数据库,为此我执行服务api调用。我在 Swift 3 中使用 Alamofire 。有23个api调用,在不同的coredata实体中提供了近24000行。

I need to sync web database in my coredata, for which I perform service api calls. I am using Alamofire with Swift 3. There are 23 api calls, giving nearly 24k rows in different coredata entities.

我的问题:这些api调用会阻塞用户界面一分钟,这对于用户来说是一个漫长的等待时间。

My problem: These api calls blocks UI for a minute, which is a long time for a user to wait.

我尝试使用 DispatchQueue 并在后台线程中执行任务,尽管没有任何效果。这是我尝试的方式:

I tried using DispatchQueue and performing the task in background thread, though nothing worked. This is how I tried :

 let dataQueue = DispatchQueue.init(label: "com.app.dataSyncQueue")
 dataQueue.async {
        DataSyncController().performStateSyncAPICall()
        DataSyncController().performRegionSyncAPICall()
        DataSyncController().performStateRegionSyncAPICall()
        DataSyncController().performBuildingRegionSyncAPICall()

        PriceSyncController().performBasicPriceSyncAPICall()
        PriceSyncController().performHeightCostSyncAPICall()

        // Apis which will be used in later screens are called in background
        self.performSelector(inBackground: #selector(self.performBackgroundTask), with: nil) 

    }

从DataSyncController进行的API调用:

 func performStateSyncAPICall() -> Void {
        DataSyncRequestManager.fetchStatesDataWithCompletionBlock {
            success, response, error in
            self.apiManager.didStatesApiComplete = true 
        }
    }

DataSyncRequestManager代码:

static func fetchStatesDataWithCompletionBlock(block:@escaping requestCompletionBlock) {

    if appDelegate.isNetworkAvailable {

        Util.setAPIStatus(key: kStateApiStatus, with: kInProgress)

        DataSyncingInterface().performStateSyncingWith(request:DataSyncRequest().createStateSyncingRequest() , withCompletionBlock: block)
    } else {
        //TODO: show network failure error
    }
}

DataSyncingInterface代码:

func performStateSyncingWith(request:Request, withCompletionBlock block:@escaping requestCompletionBlock)
 {
    self.interfaceBlock = block
    let apiurl = NetworkHttpClient.getBaseUrl() + request.urlPath!
     Alamofire.request(apiurl, parameters: request.getParams(), encoding: URLEncoding.default).responseJSON { response in

             guard response.result.isSuccess else {
               block(false, "error", nil )
               return
             }

            guard  let responseValue = response.result.value else {
                block (false, "error", nil)
                return
            }
            block(true, responseValue, nil)
        }
}

我知道许多类似的问题已经发布在Stackoverflow上,尽管尝试使用 DispatchQueues 对我没有用。

I know many similar questions have been already posted on Stackoverflow and mostly it is suggested to use GCD or Operation Queue, though trying DispatchQueues didn't work for me.

我做错了吗?
如何不阻止UI并同时执行api调用?

Am I doing something wrong? How can I not block UI and perform the api calls simultaneously?

推荐答案

您可以这样做以在后台线程上运行:

You can do this to run on a background thread:

DispatchQueue.global(qos: .background).async {

    // Do any processing you want.

    DispatchQueue.main.async {
        // Go back to the main thread to update the UI.
    }
}

这篇关于API调用会阻止UI线程Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆