     }
     
     func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
-        guard let request = dataTask.originalRequest else {
-            return
-        }
-        guard let operation = NSURLProtocol.propertyForKey(ResponseDelegateKey, inRequest: request) as? NetworkOperation else {
+        
+        guard let operation = dataTask.networkOperation else {
+        //guard let operation = NSURLProtocol.propertyForKey(ResponseDelegateKey, inRequest: request) as? NetworkOperation else {
             return
         }
         if operation.cancelled {
-         operation.finished = true
-         dataTask.cancel()
-         return
-         }
+            operation.finished = true
+            dataTask.cancel()
+            return
+        }
+        
+        if operation._incomingData == nil {
+            var capacity = -1
+
+            if let response = dataTask.response {
+                capacity = Int(response.expectedContentLength)
+            }
+            if capacity == -1 {
+                operation._incomingData = NSMutableData()
+            } else {
+                operation._incomingData = NSMutableData(capacity: capacity)
+            }
+        }
         //As the data may be discontiguous, you should use [NSData enumerateByteRangesUsingBlock:] to access it.
-        operation._incomingData.appendData(data)
-        
+        data.enumerateByteRangesUsingBlock { pointer, range, stop in
+            operation._incomingData!.appendBytes(pointer, length: range.length)
+        }
     }
     
     func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
 
-        guard let request = task.originalRequest else {
-            return
-        }
-        guard let operation = NSURLProtocol.propertyForKey(ResponseDelegateKey, inRequest: request) as? NetworkOperation else {
-            return
-        }
+        guard let operation = task.networkOperation else {
+        //guard let operation = NSURLProtocol.propertyForKey(ResponseDelegateKey, inRequest: request) as? NetworkOperation else {
+            return
+        }
+        task.networkOperation = nil
         operation.error = error
         operation.finished = true
     }

        
        let request = NSMutableURLRequest(URL: completeURL)
        
        NSURLProtocol.setProperty(self, forKey: ResponseDelegateKey, inRequest: request)
        
        _ = headers?.map { request.setValue($1, forHTTPHeaderField: $0) }
        
        let dataTask = session.dataTaskWithRequest(request)
        dataTask.resume()
    }
    
    private func encodeParameters (parameters: [String: String]) -> String {
        return (parameters.map { "\($0)=\($1)" }).joinWithSeparator("&")
    }
    
}

class ResourceSessionDelegate: NSObject, NSURLSessionDataDelegate {
    
    func urlSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
        guard let request = dataTask.originalRequest else {
            return
        }

    }
}

private let ResponseDelegateKey = "responseDelegateObject"

class NetworkOperation: NSOperation {
    
