AFNETWORK单例类,Swift

时间:2014-07-26 16:27:45

标签: swift afnetworking-2

我正在尝试在Swift中编写一个单例类来处理我使用AFNetworking的AFHTTPRequestOperationManager类的所有http请求,并遇到了我的代理使用此类以错误的顺序触发的多个请求的问题。所以我认为一个解决方案是将操作添加到队列中但似乎无法使其正常工作,收到错误说“操作已经在队列中排队”

导入UIKit

var apiURL:NSString = "http://www.api.com"

protocol NetWorkManagerDelegate {
    func requestSuccess(didSucceed response:NSArray)
    func requestFailure(didFail response:NSError)
}

class NetworkManager: AFHTTPRequestOperationManager {

    struct Singleton {
        static let sharedInstance = NetworkManager(url: NSURL(string: apiURL))
    }

    class var sharedInstance: NetworkManager {
        return Singleton.sharedInstance
    }

    var delegate:NetWorkManagerDelegate?

    init(url:NSURL)
    {
        super.init(baseURL: url)
        self.responseSerializer = AFJSONResponseSerializer()
        self.requestSerializer = AFJSONRequestSerializer()
    }

    func requestData(urlString:NSString)
    {
        var operation = self.GET(urlString,
            parameters: nil,
            success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
                self.delegate?.requestSuccess(didSucceed: responseObject as NSArray)
                println(responseObject)
            },
            failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
                self.delegate?.requestFailure(didFail: error)
                println(error)
            }
        )

        self.operationQueue.addOperation(operation)
    }


}

以下是我在视图控制器中使用该类的方法

override func viewWillAppear(animated: Bool)
{
    var networkManager:NetworkManager = NetworkManager.sharedInstance
    networkManager.delegate = self
    networkManager.requestData("categories")
}

func requestSuccess(didSucceed response: NSArray)
{
    self.menuItems = response as NSArray
    self.tableView.reloadData()
}

func requestFailure(didFail response: NSError)
{
    println("Error!")
}

1 个答案:

答案 0 :(得分:4)

我能够通过放弃委托模式并选择使用闭包作为回调来找到解决方案。这是我的新单身人士课程

import UIKit

var apiURL:NSString = "http://www.api.com"

class NetworkManager: AFHTTPRequestOperationManager {

    struct Singleton {
        static let sharedInstance = NetworkManager(url: NSURL(string: apiURL))
    }

    class var sharedInstance: NetworkManager {
        return Singleton.sharedInstance
    }

    init(url:NSURL)
    {
        super.init(baseURL: url)

        if (self != nil) {
            self.responseSerializer = AFJSONResponseSerializer()
            self.requestSerializer = AFJSONRequestSerializer()
        }

    }

    func requestData(urlString:String, completion:(data:NSArray?, error:NSError?) -> ())
    {
        var operation = self.GET(urlString,
            parameters: nil,
            success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
                completion(data: (responseObject as NSArray), error: nil)
            },
            failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
                completion(data: nil, error: error)
            }
        )
    }


}

这是我的视图控制器,请注意requestData方法现在实现了一个闭包作为回调

    override func viewWillAppear(animated: Bool)
    {
        var networkManager:NetworkManager = NetworkManager.sharedInstance
        networkManager.requestData("categories") {
            (data:NSArray?, error:NSError?) -> () in
            if (data != nil) {
                self.requestSuccess(didSucceed: data!)
            } else if (error != nil) {
                self.requestFailure(didFail: error!)
            }
        }
    }

    func requestSuccess(didSucceed response: NSArray)
    {
        self.menuItems = response as NSArray
        self.tableView.reloadData()
    }

    func requestFailure(didFail response: NSError)
    {
        println("Error!")
    }

如果有人知道更好的方式让我知道,只需在这里学习Swift。