Swiftpack.co -  ralfebert/PersistentURLRequestQueue as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A simple serial, persistent URLRequest queue.
.package(url: "https://github.com/ralfebert/PersistentURLRequestQueue.git", from: "0.1.8")


This package provides a serial persistent URLRequest queue: Send requests to the backend server, but if the app is offline or a timeout/error happens, persist the request and retry it later after a while.


This component is supposed to be used for the special case of sending requests that must not be lost. For example: selling something in the app, which can be done offline, but the backend needs to know about it eventually. The server is responsible for handling every request, otherwise it will be tried again and again (that's intentional).

This solution is only applicable if you can persist the whole URLRequest in the app's document directory. If you need to store credentials in a more secure fashion or need to handle things like retrying with an access token that might have expired, this solution will not work.

Setting up a queue

let queue = PersistentURLRequestQueue(name: "Tasks", urlSession: .shared, retryTimeInterval: 30)

Short version:

let queue = PersistentURLRequestQueue(name: "Tasks")

Enqueuing a request

var request = URLRequest(url: URL(string: "http://www.example.com")!)
request.httpMethod = "POST"

This request will be immediately persisted and PersistentURLRequestQueue will try to send it until it got a HTTP Success (200) response code. If a request fails, it will be retried after retryTimeInterval. It's a serial queue, only one request will be processed at a time.

Observing the queue

You can also be notified if the request finishes immediately in the current app session:

queue.add(request) { data, response in
    os_log("Request was sent.")

You can ask for the number of entries still outstanding:

let count = queue.allEntriesCount()

PersistentURLRequestQueue is a SwiftUI ObservableObject that sends a change notification when the number of entries changes.

Manually starting a queue run

It's recommended to start a queue run when the app appeared or when the reachability of the device changes (Reachability.swift helps with that):

queue.startProcessing(ignorePauseDates: true)


Stars: 3
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API