Latest 0.2.0
Homepage https://github.com/lucianomarisi/LoadIt
License MIT The MIT License (MIT)
Copyright (c) 2016 Luciano Marisi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Platforms ios 8.0
Authors

THIS IS REPOSITORY IS NOW DEPRECATED, USE IN TABResourceLoader INSTEAD

Build Status
LoadIt
LoadIt
codecov.io
Documentation

The idea behind this library is for the user to define the resources in their application by conforming to protocols that define where and how to get them. These resource can then be retrieved using a generic service type with or without an operation provided by the library.

Note that this library is still under development and so at the moment the only supported resource type is JSON.

The original idea for this pattern is explained on Protocol oriented loading of resources from a network service in Swift

Simple example

Let’s say you need to retrieve this JSON from an server, http://localhost:8000/<continent>.json:

{
  "cities": [{
    "name": "Paris"
  }, {
    "name": "London"
  }]
}

The Swift model for a city could look something like this:

struct City {
  let name: String
}

Some basic parsing logic:

extension City {
  init?(jsonDictionary: [String : AnyObject]) {
    guard let parsedName = jsonDictionary["name"] as? String else {
      return nil
    }
    name = parsedName
  }
}

Defining a Resource

A CitiesResource can be created to define where the cities will come from and how the base endpoint should be parsed:

private let baseURL = NSURL(string: "http://localhost:8000/europe.json")!

struct CitiesResource: NetworkJSONResourceType {
  typealias Model = [City]

  let url: NSURL

  init(continent: String) {
    url = baseURL.URLByAppendingPathComponent("(continent).json")
  }

  //MARK: JSONResource
  func modelFrom(jsonDictionary jsonDictionary: [String: AnyObject]) -> [City]? {
    guard let
      citiesJSONArray = jsonDictionary["cities"] as? [[String: AnyObject]]
      else {
        return []
    }
    return citiesJSONArray.flatMap(City.init)
  }

}

Retrieving the resource

Use the provided NetworkJSONResourceService to retrieve your CitiesResource from a web service:

let europeResource = CitiesResource(continent: "europe")
let networkJSONService = NetworkJSONResourceService<CitiesResource>()
networkJSONService.fetch(resource: europeResource) { [weak self] result in
  // do something with the result
}

OR

Define a typealias for conveniency if you using NSOperations:

private typealias CitiesNetworkResourceOperation = ResourceOperation<NetworkJSONResourceService<CitiesResource>>

Create the operation using a CitiesResource

let europeResource = CitiesResource(continent: "europe")
let citiesNetworkResourceOperation = CitiesNetworkResourceOperation(resource: europeResource) { [weak self] operation, result in
  // do something with the result
}

Add the operation to some queue

let operationQueue = NSOperationQueue()
operationQueue.addOperation(citiesNetworkResourceOperation)

Creating your own services and operations

At the moment the only service provided is the NetworkJSONResourceService. This will change in future updates where it may be relevant to ship this library with more default services. In the meantime the user can create they own service by conforming to ResourceServiceType. Similarly, even though the ResourceOperation may cater for most needs the developer can choose to have their own resource operation that conforms to ResourceOperationType.

Latest podspec

{
    "name": "LoadIt",
    "version": "0.2.0",
    "license": {
        "type": "MIT",
        "text": "    The MIT License (MIT)n    Copyright (c) 2016 Luciano Marisin    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:n    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.n    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.n"
    },
    "homepage": "https://github.com/lucianomarisi/LoadIt",
    "authors": {
        "Luciano Marisi": "[email protected]"
    },
    "summary": "Define resources in protocols and load them using a generic service and operation",
    "source": {
        "git": "https://github.com/lucianomarisi/LoadIt.git",
        "tag": "0.2.0"
    },
    "source_files": "Sources/LoadIt/**/*.swift",
    "platforms": {
        "ios": "8.0"
    },
    "deprecated_in_favor_of": "TABResourceLoader",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This