Coder Social home page Coder Social logo

augmify / overdrive Goto Github PK

View Code? Open in Web Editor NEW

This project forked from saidsikira/overdrive

0.0 0.0 0.0 1.44 MB

⚡️ Fast async task based API in Swift with focus on type safety, concurrency and multi threading

Home Page: swiftable.io/overdrive

License: MIT License

Ruby 0.81% Swift 93.08% Objective-C 0.69% Shell 5.42%

overdrive's Introduction

Build Status ![Plaforms](https://img.shields.io/badge/platform- iOS | macOS | tvOS | linux-gray.svg) Carthage Compatible Swift Package Manager Compatible

Our apps constantly do work. The faster you react to user input and produce an output, the more likely is that the user will continue to use your application. As our applications grow in complexity, the more and more work needs to be done. You need to start thinking about how to categorize and optimize work, how to make that work more efficient, more optimized and finally, faster. In most cases that doesn’t end very well because you need to know a lot about concurrency, multithreading etc. - it’s a very complex field. You need to know all API specifics before you are able to write something.

Overdrive was created as a result of that struggle. It is a framework that exposes several simple concepts which are made on top of complex system frameworks that enable multithreading, concurrency and most importantly, more speed.

let task = URLSessionTask("https://api.swiftable.io")

task
  .retry(3)
  .onValue { json in
    print(json["message"])
  }.onError { error in
    print(error)
  }

TaskQueue.background.add(task: task)

Contents:

What can I do with Overdrive?

  • Execute tasks concurrently
  • Utilize multi-core systems by default
  • Easily defer task execution to custom thread or queue
  • Ensure that multiple tasks are executed in the correct order
  • Express custom conditions under which tasks can be executed
  • Enforce testability
  • Retry tasks that finished with errors
  • Write thread safe code by default

Requirements

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.0+
  • Swift 3

Installation

Carthage

github "arikis/Overdrive" >= 0.0.2

Cocoa Pods

platform :ios, '8.0'
use_frameworks!

target 'AppTarget' do
    pod 'Overdrive'
end

Swift Package Manager

import PackageDescription

let package = Package(
  name: "Your Package Name",
  dependencies: [
    .Package(url: "https://github.com/arikis/Overdrive.git",
            majorVersion: 0,
            minorVersion: 2)
  ]
)

Manual installation

Overdrive can also be installed manually by dragging the Overdrive.xcodeproj to your project and adding Overdrive.framework to the embedded libraries in project settings.

Usage

Overdrive features two main classes:

Workflow:

  1. Create subclass of Task<T>
  2. Override run() method and encapsulate any synchronous or asynchronous operation
  3. Finish execution with value(T) or error(Error) by using finish(with:) method
  4. Create instance of subclass
  5. Add it to the TaskQueue when you want to start execution

Example Task<UIImage> subclass for photo download task:

class GetLogoTask: Task<UIImage> {

  override func run() {
      let logoURL = URL(string: "https://swiftable.io/logo.png")!

      do {
          let logoData = try Data(contentsOf: logoURL)
          let image = UIImage(data: logoData)!
          finish(with: .value(image)) // finish with image
      } catch {
          finish(with: .error(error)) // finish with error if any
      }
  }
}

To setup completion blocks, you use onValue and onError methods:

let logoTask = GetLogoTask()

logoTask
    .onValue { logo in
        print(logo) // UIImage object
    }.onError { error in
        print(error)
}

To execute the task add it to the instance of TaskQueue

let queue = TaskQueue()
queue.add(task: logoTask)

Concurrency

TaskQueue executes tasks concurrently by default. Maximum number of concurrent operations is defined by the current system conditions. If you want to limit the number of maximum concurrent task executions use maxConcurrentTaskCount property.

let queue = TaskQueue()
queue.maxConcurrentTaskCount = 3

Thread safety

All task properties are thread-safe by default, meaning that you can access them from any thread or queue and not worry about locks and access synchronization.

Inspiration

Overdrive framework was heavily inspired by talks and code from several Apple WWDC videos.

Overdrive is a term for an effect used in electric guitar playing that occurs when guitar amp tubes starts to produce overdriven, almost distorted sound, due to the higher gain(master) setting.

overdrive's People

Contributors

arikis avatar saidsikira avatar

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.