From 1b9441c37944e180fd6f1fdaea28729c6d8bc01c Mon Sep 17 00:00:00 2001 From: Alasdair Law Date: Mon, 19 Dec 2016 23:35:05 +0000 Subject: [PATCH] Add README.md --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/README.md b/README.md index 006fa1f..bc6387b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,69 @@ # network-operation NSOperation subclass for network requests + +## Requirements + +- Swift 3.0.1 +- Xcode 8.1+ + +## Installation + +### Embedded Framework + +- Add as a submodule to your git repository +- Add NetworkOperation.framework to your application's Xcode project +- Add NetworkOperation.framework to the project's Embedded Binaries + +## Usage + +### Making a Request + +Example: + +```swift +import NetworkOperation + +let operationQueue = OperationQueue() + +let session = URLSession.shared +let url = URL(string: "https://jsonplaceholder.typicode.com/posts")! +let request = URLRequest(url: url) + +let operation = NetworkOperation(session: session, request: request) { (response: JSONResponse?, error) in + guard let posts = response?.data as? [[String: Any]] else { + return + } + use(posts: posts) +} + +operationQueue.addOperation(operation) +``` + +NetworkOperations can be scheduled in a queue just like any other NSOperation subclass. + +#### Responses + +Instances confirming to `NetworkResponse` allow you to control how the Data returned from `NetworkOperation` is serialised. + +For example, the provided JSONResponse struct serialises the Data returned into Any, where Any is a JSON array or dictionary. + +```swift +public struct JSONResponse: NetworkResponse { + public typealias T = Any + + public let response: URLResponse + public let data: T + + public init(response: URLResponse, data: Data) throws { + self.response = response + + self.data = try JSONSerialization.jsonObject(with: data) + } +} +``` + +Custom `NetworkResponse` types are provided through the `NetworkOperation`'s generic type parameter. + +## License + +network-operation is released under the MIT license. See LICENSE for details. \ No newline at end of file