Home

Awesome

Bismuth

Bismuth is part of the E-sites iOS Suite.


A lightweight framework to handle queues

forthebadge forthebadge

Platform CocoaPods Compatible Carthage compatible Travis-ci

Installation

Podfile:

pod 'Bismuth'

And then

pod install

Implementation

import Bismuth
import Alamofire

extension Alamofire.HTTPMethod: Codable { }

struct RequestQueueItem: BismuthQueueable {
    private enum CodingKeys: String, CodingKey {
        case url
        case httpMethod
        case parameters
    }
    
    private let uuid = UUID().uuidString

    let url: String
    let httpMethod: HTTPMethod
    let parameters: [String: String]?

    init(url: String, httpMethod: HTTPMethod = .get, parameters: [String: String]? = nil) {
        self.url = url
        self.httpMethod = httpMethod
        self.parameters = parameters
     }

    static func == (lhs: RequestQueueItem, rhs: RequestQueueItem) -> Bool {
        return lhs.uuid == rhs.uuid
    }
}
func configure() {
	var config = Bismuth.Config(identifier: "api-requests")
	config.logProxy = { line in
	    print("[REQ] \(line)")
	}
	config.autoStart = false // Default: true
	config.canRunInBackground = false // Default: true
	config.retryTime = 30 // Default: 15
	
	queue = Bismuth.Queue<RequestQueueItem>(config: config)
	queue.delegate = self
	queue.add(RequestQueueItem(url: "https://domain.com/api/get"))
	queue.add(RequestQueueItem(url: "https://domain.com/api/post", httpMethod: .post, parameters: [ "name": "Bas" ]))
	queue.add(RequestQueueItem(url: "https://domain.com/api/items/3", httpMethod: .delete))
}

// BismuthQueueDelegate functions

func queue<T>(_ queue: Bismuth.Queue<T>, handle item: T, attempts: Int, completion: @escaping (Bismuth.HandleResult) -> Void) where T : BismuthQueueable {
	guard let item = item as? RequestQueueItem else {
        completion(.handled)
        return
    }
    
    // Do stuff with the queue item
    Alamofire.request(item.url, method: item.httpMethod, parameters: item.parameters)
    .validate()
    .responseJSON { response in
         switch response.result {
         case .success:
             completion(.handled)
         case .failure(let error):
             if attempts >= 5 {
                 completion(.handled)
             } else {
                 completion(.retry)
             }
        }
    }
}

func queueFinished<T>(_ queue: Bismuth.Queue<T>) where T : BismuthQueueable {

}

Configuration

KeyTypeDescriptionDefault
autoStartBoolDo you want the queue to auto start upon app activation?true
reryTimeIntervalTimeIntervalFailed queue items will retry after x seconds15
canRunInBackgroundBoolCan the queue operate in the backgroundtrue
logProxy(String) -> VoidA closure to call for debug loggingnil

Control

Pause a queue until resume() is called

func pause()

Resumes a queue that was paused using pause()

func resume()

Starts a queue, that is idle

func start()

The current state of the queue (idle, paused or running)

var state: Bismuth.QueueState