Skip to content

Commit

Permalink
Add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
alasdairlaw committed Dec 19, 2016
1 parent 30a49e4 commit 1b9441c
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<JSONResponse>(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.

0 comments on commit 1b9441c

Please sign in to comment.