Giter VIP home page Giter VIP logo

loadit's Introduction

THIS IS REPOSITORY IS NOW DEPRECATED, USE IN TABResourceLoader INSTEAD

LoadIt

Build Status 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.

loadit's People

Contributors

lucianomarisi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

loadit's Issues

Swift 3 compilation error for ResourceOperation

Under Xcode 8 with Swift 3, compiling ResourceOperation.swift yields the following error message:

<unknown>:0: error: same-type constraint type 'τ_0_0.Resource' does not conform to required protocol 'ResourceType'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.