2015-11-17 75 views
7

我在將轉換成3.0轉換成ReactiveCocoa時遇到了很多麻煩。在我的sendNextsendCompleted調用中,我總是收到未知的標識符錯誤。Alamofire 3.0 ReactiveCocoa Conversion

public final class Network: Networking { 
private let queue = dispatch_queue_create("Beet.BeetModel.Network.Queue", DISPATCH_QUEUE_SERIAL) 

public init() { } 

public func requestJSON(url: String, parameters: [String : AnyObject]?) 
    -> SignalProducer<AnyObject, NetworkError> 
{ 
    return SignalProducer { observer, disposable in 
     let serializer = Alamofire.Request.JSONResponseSerializer() 
     Alamofire.request(.GET, url, parameters: parameters) 
      .response(queue: self.queue, responseSerializer: serializer) { 
       _, _, result in 
       switch result { 
       case .Success(let value): 
        sendNext(observer, value) 
        sendCompleted(observer) 
       case .Failure(_, let error): 
        sendError(observer, NetworkError(error: error)) 
       } 
     } 
    } 
} 
} 

回答

2

嘗試observer.sendNext(value)和同上,用於sendCompletedsendError

11

這句法changed in 4.0 alpha 2Observer現在是它自己的類型,這樣老功能sendNextsendError等不再是免費的功能:

switch result { 
case .Success(let value): 
    observer.sendNext(value) 
    observer.sendCompleted() 
case .Failure(_, let error): 
    observer.sendError(NetworkError(error: error)) 
} 

有一件事我會添加到您的解決方案是提供一個一次性的,這樣的要求如果需要可以取消,節省資源:

return SignalProducer { observer, disposable in 
    let serializer = Alamofire.Request.JSONResponseSerializer() 
    let request = Alamofire.request(.GET, url, parameters: parameters) 

    request.response(queue: self.queue, responseSerializer: serializer) { _, _, result in 
     switch result { 
     case .Success(let value): 
      observer.sendNext(value) 
      observer.sendCompleted() 
     case .Failure(_, let error): 
      observer.sendError(NetworkError(error: error)) 
     } 
    } 

    disposable.addDisposable(request.cancel) 
}