如何使用NSOperationQueue进行同步请求?

时间:2016-08-04 07:37:52

标签: ios swift nsurlsession

我是开发ios应用程序的新手,我正在尝试连接https web服务器,我遇到了问题我无法连接自签名证书,所以我搜索了网络我已经按照下面给出的代码进行了工作精细。但它的工作异步模式。所以我需要等到完成任务等待并返回数据。请给出任何想法?/

func requestAsynChronousData(request: NSURLRequest)->NSData? {
    let data: NSData? = nil
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
    let task = session.dataTaskWithRequest(request){ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
      if error == nil {
        print("RESULTTTT\(data)")
        }
    }
    task.resume()
    return data;
}

func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
    print("WAITING!!!!!!!!!!!!!COMPLETIONHANDLER")
    completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
}

3 个答案:

答案 0 :(得分:0)

您需要将dispatch_semaphore_create添加到您的代码中,如下所示

func requestAsynChronousData(request: NSURLRequest)->NSData? {
    let data: NSData? = nil
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()

    let semaphore = dispatch_semaphore_create(0)

    let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
    let task = session.dataTaskWithRequest(request){ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in

      if error == nil {
        print("RESULTTTT\(data)")
      }

      dispatch_semaphore_signal(semaphore)
    }
    task.resume()

    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    return data;
}

但是这种方式会在你的完成处理程序完成时阻止你的主线程

答案 1 :(得分:0)

我找到了解决方案。

func requestAsynChronousData(request: NSURLRequest)->NSData? {
let data: NSData? = nil
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:nil)
let task = session.dataTaskWithRequest(request){ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
  if error == nil {
    data=taskData
    print("RESULTTTT\(data)")
    }
    dispatch_semaphore_signal(semaphore)
}
task.resume()
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return data; 
}

func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
print("WAITING!!!!!!!!!!!!!COMPLETIONHANDLER")
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
}

答案 2 :(得分:0)

或者,您可以将队列上的最大操作数设置为1。

let operationQueue = NSOperationQueue()
operationQueue.maxConcurrentOperationCount = 1

let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:operationQueue)
let task = session.dataTaskWithRequest(request){ (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in

  if error == nil {
    print("RESULTTTT\(data)")
  }
}

operationQueue.addOperation(task)

这将强制此队列中的操作同步操作,而不使用允许NSOperationQueue自行清理的信号量