Latest 0.0.2
Homepage https://github.com/jianstm/Once
License MIT
Platforms ios 8.0, osx 10.10, watchos 2.0, tvos 9.0, requires ARC
Frameworks Foundation
Authors

Once(简体中文)

[![Build Status](https://travis-ci.org/jianstm/Once.svg?branch=master)](https://travis-ci.org/jianstm/Once)
[![codecov](https://codecov.io/gh/jianstm/Once/branch/master/graph/badge.svg)](https://codecov.io/gh/jianstm/Once)
OnceOnceOnce

Executes your task once and only once.

Features

  • [x] Safe
  • [x] Efficient
  • [x] Persistent
  • [x] Minimalist
  • [x] 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 initSomething() {
    Once.run {
        // No matter how many times `initSomething` 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 judge 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
  • .until(let until): to until
  • .every(let period): every 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"))
]

Latest podspec

{
    "name": "Once",
    "version": "0.0.2",
    "summary": "Minimalists library to manage one-off operations.",
    "description": "Once is a minimalists library to manage one-off operations.",
    "homepage": "https://github.com/jianstm/Once",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Quentin Jin": "[email protected]"
    },
    "source": {
        "git": "https://github.com/jianstm/Once.git",
        "tag": "0.0.2"
    },
    "source_files": "Sources/**/*",
    "frameworks": "Foundation",
    "requires_arc": true,
    "swift_version": "4.2",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "2.0",
        "tvos": "9.0"
    }
}

Pin It on Pinterest

Share This