Git Product home page Git Product logo

retry's Introduction

Retry

Version License Platform

Example

Haven't you wished for try to sometimes try a little harder? Meet retry

To run the example project, clone the repo, and run pod install from the Example directory first.

The full test suite shows a variety of use cases.

Synchronous retry

NB: The sync retry is not good for use on the main thread (because it will block it if you're having delays, etc). For the main thread it's better considering retryAsync (more info below).

Default parameters - retry three times without any delay:

retry {
  ... do some throwing work ...
}

Catching the last error, and adding a defer block after all tries have finished - will keep trying maximum 10 times. You can use either of the final blocks or both:

retry (max: 10) {
  ... do some throwing work ...
}
.finalCatch {lastError in
  print("This simply won't happen. Failed with: \(lastError)")
}
.finalDefer {
  print("Finished trying")
}

Add 2 second delay between the retries:

retry (max: 5, retryStrategy: .delay(seconds: 2.0)) {
  ... do some throwing work ...
}

Implement any custom delay logic (below is multiplying the waiting time after each try):

retry (max: 5, retryStrategy: .custom {count, lastDelay in return (lastDelay ?? 1.0) * 2.0} ) {
  ... do some throwing work ...
}

Limit the number of retries based on any custom logic - if you return nil from your custom strategy, retry will stop trying:

retry (max: 5, retryStrategy: .custom {count,_ in return count > 3 ? nil : 0} ) {
  ... do some throwing work ...
}

Asynchronous retry

The syntax for retryAsync is exactly the same as for retry. The difference in behavior is if the first try fails retryAsync keeps trying asynchronously instead of blocking the current thread.

Installation

Retry is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Retry"

Since Retry is a swift3 library, you got to add this piece of code to your project's Podfile, to update your targets' swift language version:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '3.0'
            config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '10.10'
        end
    end
end

Author

Marin Todorov, 2016-present

Inspired by the retry operator in https://github.com/RxSwiftCommunity/RxSwiftExt

License

Retry is available under the MIT license. See the LICENSE file for more info.

retry's People

Contributors

icanzilb avatar polac24 avatar

Stargazers

 avatar

Watchers

 avatar  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.