• Stars
    star
    498
  • Rank 88,494 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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"

Retry is also available through Swift Package Manager. To install, add https://github.com/icanzilb/Retry.git to your package manifest.

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.

More Repositories

1

EasyAnimation

A Swift library to take the power of UIView.animateWithDuration(_:, animations:...) to a whole new level - layers, springs, chain-able animations and mixing view and layer animations together!
Swift
2,960
star
2

SwiftSpinner

A beautiful activity indicator and modal alert written in Swift (originally developed for my app DoodleDoodle) Using blur effects, translucency, flat and bold design - all iOS 8 latest and greatest
Swift
2,184
star
3

Timelane

Timelane
Swift
706
star
4

TaskQueue

A Task Queue Class developed in Swift (by Marin Todorov)
Swift
681
star
5

UIEffectDesignerView

A native Particle Systems effect view for iOS and OSX powered by QuartzCore
Objective-C
617
star
6

EventBlankApp

A free open source iOS app for events or conferences. Read more on the app's webpage:
Swift
291
star
7

MTLog

NSLog replacement for coders!
Objective-C
222
star
8

Breadcrumbs

Bugtracker working off source code
Swift
193
star
9

TimelaneCombine

Timelane + Combine
Swift
168
star
10

RxTimelane

Timelane + RxSwift
Swift
132
star
11

PowerUpYourAnimations

Sample code from talks on advanced animations
Swift
118
star
12

DoNilDisturbPlugin

A plugin for your Xcode project that stops you from working outside work hours
Swift
109
star
13

timeui

Profile apps from the command line — duration, cpu & memory usage.
Swift
107
star
14

OneShotLocationManager

A replacement class for CLLocationManager for when you want to easily fetch the current device location
Swift
101
star
15

MTPopupWindow

Popup-window style view for Objective-C, which loads contents of an HTML file. Easy one-line usage. Check the readme for example
Objective-C
84
star
16

TimelaneCore

Timelane + Core
Swift
66
star
17

OperationTimelane

Timelane + Operations
Swift
45
star
18

RxSwiftoniOS

Sample code from my dotSwift 2017 talk in Paris
Swift
43
star
19

PackageView

An app that displays Package.swift info
Swift
42
star
20

RealmGitHubSearchRxDemo

The demo app for RxRealm's post on realm.io
Swift
36
star
21

RealmMultiplatformDemo

Demo that shares its model layer across Apple's four platforms
Swift
36
star
22

Advanced-RSS-reader

An example of an RSS reader app for iPhone, full source and comments
Objective-C
34
star
23

MTTestSemaphore

A class to help you create unit tests that test asynchronous methods. You will need this to unit test any class that fetch data from the network, use location, camera, etc.
Objective-C
33
star
24

Unxippity

Unxips quickly Xcode downloads
Swift
32
star
25

Cancellor

Bind multiple cancellables to the lifetime of another object like a view controller.
Swift
32
star
26

RealmNotificationExample

The project for the post demonstrating fine grained notifications on realm.io
Swift
30
star
27

MarkWalkthrough

A SwiftUI package to quickly build app walkthroughs
Swift
27
star
28

HUD

Beautiful alert message/ progress hud component for iOS Objective-C
Objective-C
25
star
29

SafariDownload

Swift package to read Safari's download packages
Swift
8
star
30

CustomInstrument

A custom Xcode instrument based on Timelane
Swift
7
star
31

Fetch-and-parse-JSON

Fetch and parse JSON
Objective-C
7
star
32

ActorBench

Actor vs queue vs lock benchmark
Swift
6
star
33

TimelaneInstrument

Timelane Tools Instrument
5
star
34

StylesDemoApp

A little experiment on styling SwiftUI views
Swift
4
star
35

powerups

cli for dynamic XML includes
Swift
3
star
36

nsspain2020

Demo projects from my talk at NSSpain 2020
Swift
3
star
37

Tracker

Google Analytics iOS shortcut wrapper
3
star
38

TimerApp

Timer App v1
Swift
3
star
39

LogRider

Logs viewer app for mac
Swift
3
star
40

snippetty.io

snippetty.io
HTML
3
star
41

HTTPKit

Task based, promise like syntax, RESTful, HTTP library for iOS and OS X. Built off ConcurrentKit and NSURLSession.
Objective-C
2
star
42

react-native-console-oslog

React Native package to log to Apple's unified log
Objective-C++
2
star
43

rx-marin

rx blog:
HTML
2
star
44

combinebook.com

HTML
2
star
45

ios-animations-by-emails

iOS Animations by Emails newsletter archive
HTML
2
star
46

snippety.io

snippetty.io
1
star
47

swiftconcurrencybook

swiftconcurrencybook.com
CSS
1
star
48

AnagramsGameiPad

The completed source code to "How to create an awesome Anagrams game with UIKit"
Objective-C
1
star
49

Baffle

Ruby
1
star
50

www.timelane.tools

www.timelane.tools
1
star
51

macro-bad-access

Swift
1
star
52

tryCombine

tryCombine blog by Marin Todorov
HTML
1
star
53

Languages

RWDevCon tutorial code
Swift
1
star