IOS,Swift,无法访问NSURLSession中的主线程

时间:2015-02-03 19:17:34

标签: ios swift grand-central-dispatch nsurlsession

我正在使用swift创建一个iOS应用。我想实现一些GET或POST HTTP请求。我知道Alamofire存在,但我想创建自己的功能。

我做了什么:

import Foundation

class DatabaseRequest:NSObject{

    class func GET(urlAsString:String, completion : (response:NSURLResponse, result:AnyObject?, error:String?)-> Void){

        let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
        let session = NSURLSession(configuration: configuration)

        let url = NSURL(string: urlAsString)
        let urlRequest = NSMutableURLRequest(URL: url!)
        urlRequest.HTTPMethod = "GET"

        session.dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) -> Void in
            if let error = error{
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    completion(response: response, result: nil, error: "GET Connection error : \(error.localizedDescription)")
                })
            }else{
                var error:NSError?

                let JSON_Object:AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)
                if let error = error{
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        completion(response: response, result: nil, error: "GET JSONSerialization error: \(error.localizedDescription)")
                    })
                }else{
                    if let result:AnyObject = JSON_Object{
                        //The third time I use this class function, no way to access the main thred to send data
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            completion(response: response, result: JSON_Object, error: nil)
                        })
                    }else{
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            completion(response: response, result: nil, error: nil)
                        })
                    }
                }
            }

            session.finishTasksAndInvalidate()

        }).resume()

    }

    class func POST(urlAsString:String, parameters:[String:AnyObject], completion:(response:NSURLResponse?, result:AnyObject?, error:String?)->Void){
        println("POST used")
        let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
        let session = NSURLSession(configuration: configuration)

        var errorHTTPBody:NSError?
        let url = NSURL(string: urlAsString)
        let urlRequest = NSMutableURLRequest(URL: url!)
        urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
        urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
        urlRequest.HTTPMethod = "POST"
        urlRequest.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &errorHTTPBody)

        if let error = errorHTTPBody{
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                completion(response: nil, result: nil, error: "POST errorHTTPBody: \(error.localizedDescription)")
            })
            return
        }

        session.dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) -> Void in
            println(data)
            if let error = error{
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    completion(response: response, result: nil, error: "POST Connection error : \(error.localizedDescription)")
                })
            }else{
                var error:NSError?
                var JSON_Object:AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)
                if let error = error{
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        completion(response: response, result: nil, error: "POST JSONSerialization error : \(error.localizedDescription)")
                    })
                }else{
                    if let result:AnyObject = JSON_Object{
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            completion(response: response, result: JSON_Object, error: nil)
                        })
                    }else{
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            completion(response: response, result: nil, error: nil)
                        })
                    }
                }
            }

            session.finishTasksAndInvalidate()

        }).resume()

    }

}

有趣的部分主要是GET功能(因为POST功能的工作方式相同)。好吧,一切似乎都很好。我实现了2次使用这个GET功能,但第三次我想使用它,没有办法访问主线程来发送数据。我可以在我的评论//The third time I use this class function, no way to access the main thread to send data之前记录一些内容,但没有任何内容记录在dispatch_async(dispatch_get_main_queue(),block)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你在这里尝试的对我来说似乎有点奇怪。您将从主线程异步调度到主线程。通常你会派遣异步来在另一个线程上同时执行某些事情而不是当前线程,这样当前线程可以在不必等待异步任务完成的情况下继续执行它。当主线程无关时,调度到同一个线程将(我的猜测)将任务排队等待执行。我不明白你为什么要在主线程上执行这些任务。只要您不尝试操纵UI对象,任何线程都可以。我真的只会在触摸UI时使用主线程。 现在谈到执行HTTP提取或发布的问题,我建议遵循Apple传播的方法。这是使用委托来处理异步回调。 Apple已定义了三个委托协议:NSURLSessionDelegate,NSURLSessionTaskDelegate,NSURLSessionDataDelegate。我会创建一个类,比如实现这些协议的HTTPClient:

@interface HTTPClient : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>

@property (strong, nonatomic) NSURLSession *session;

@property (strong, nonatomic) NSMutableDictionary *runningTasks;


- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data;

- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error;

- (NSURLSessionTask *) startGetTaskForURL: (NSURL *) url;

@end

@implementation HTTPClient

- (NSURLSessionTask *) startGetTaskForURL: (NSURL *) url {
    NSURLSessionTask *task = [self.session dataTaskWithURL:url];
    NSMutableData *data = [NSMutableData data];
    [task resume];
    [self.runningTasks setObject:data forKey:task];
    return task;
}

- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *) dataTask didReceiveData: (NSData *)data {
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSMutableData *runningData = [self.runningTasks objectForKey:task];
    if (!runningData) {
        NSLog(@"No data found for task");
    }
    [runningData appendData: data];
}

- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
    NSData *data = [self.runningTasks objectForKey:task];
    //process the data received
}

@end

收到所有数据后,您可以执行必要的JSON处理。 当然你需要初始化运行Tasks的字典。