🚀 Minimalist library to manage one-off operations.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Configs README Oct 6, 2018
Once.xcodeproj Init Oct 6, 2018
Sources/Once Fix making token Oct 16, 2018
Tests Fix making token Oct 16, 2018
.codecov.yml README Oct 6, 2018
.gitignore Init Oct 6, 2018
.swift-version README Oct 6, 2018
.swiftlint.yml Init Oct 6, 2018
.travis.yml README Oct 6, 2018
LICENSE Init Oct 6, 2018
Once.podspec Update to 0.0.2 Oct 16, 2018
Package.swift README Oct 6, 2018
README.md Update README.md Nov 1, 2018
README.zh_cn.md Add contributing description Oct 30, 2018

README.md

Once(简体中文)




Executes your task once and only once.

Highlight

  • Safe
  • Efficient
  • Persistent
  • Minimalist
  • Intuitive

Usage

Run

Once.run will execute your task once and only once during the lifetime of application, and no need to initialize a flag in advance~ 😉

func doSomethingOnlyOnce() {
    Once.run {
        // No matter how many times `doSomethingOnlyOnce` is called, 
        // the message will only be printed once.
        // In multithreading, if the task is executing, 
        // the subsequent thread will wait for the execution ends.
        print("Once!")
    }
}

If you want to check if the same task has already been executed elsewhere, you can use token:

var i = 0
let token = Once.makeToken()

// a.swift
Once.run(token) {
    i += 1
}

// b.swift
Once.run(token) {
    // No matter how many places it is called, the variable will only increment once.
    i += 1
}

Do

Unlike run, do will persist the execution history of the task (using UserDefault).

Before moving on to do, let's get to know a few simple concepts:

Period

Period represents a time period, its common usage is as follows:

let ago = Period.minute(30).ago  // 30 minutes ago

let p0: Period = .year(1)
let p1: Period = .month(2)
let p2: Period = .day(3)

let p3 = p0 + p1 + p2
let later = p3.later

Scope

Scope represents a time range, it is an enum:

  • .install: from app installation
  • .version: from app update
  • .session: from app launch
  • .since(let since): from since(Date)
  • .until(let until): to until(Date)
  • .every(let period): every period(Period)

Let's take a look at do:

let showTutorial = Label(rawValue: "show tutorial")

Once.do(showTutorial, scope: .version) { (sealer) in
    app.showTutorial()
    
    // You always need to call `seal` to mark the task as done, 
    // otherwise the execution will not be logged.
    // Same as `do`, in multithreading, if the task is executing, 
    // the subsequent thread will wait for the execution ends.
    sealer.seal() 
}

Once.if("remind", scope: .session, times: .lessThan(3)) { (sealer) in
    app.remind()
    sealer.seal()
}

Once.unless("pop ad", scope: .session, times: .equalTo(5)) { (sealer) in
    app.popAd()
    sealer.seal()
}

// Clear the history of the task.
Once.clear("pop ad")

// Date of the last execution.
Once.lastDone(of: "pop ad")

Installation

CocoaPods

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
  pod 'Once', '~> 0.0.1'
end

Carthage

github "jianstm/Once" ~> 0.0.1

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/jianstm/Once", .upToNextMinor(from: "0.0.1"))
]

Contributing

Encounter a bug? want more features? Feel free to open an issue or submit a pr directly!