Skip to content

Instantly share code, notes, and snippets.

@nashysolutions
nashysolutions / JobQueue.swift
Created January 5, 2026 03:41
A MainActor-isolated, FIFO job queue that deduplicates enqueued jobs and processes them sequentially via an async handler, ensuring only one draining task runs at a time.
/// A simple FIFO job queue that serially processes unique jobs using an async handler.
///
/// `JobQueue` ensures only one draining task runs at a time. When a job is enqueued,
/// the queue starts a task on the main actor to
/// drain jobs in order. Duplicate jobs (as defined by `Equatable`) are ignored when
/// enqueuing.
///
/// - Important: This queue is `@MainActor`-isolated. The `handler` runs on the main
/// actor. Offload heavy work from the handler to a background context to avoid UI
/// jank.
# Privacy Policy for QueryPeeler
QueryPeeler does not collect, store, or transmit any personal data.
The app runs entirely on your device and does not send any information to external servers. It does not collect analytics, track user behaviour, or access sensitive information.
If you have any concerns, please contact support@robertnash.dev.
@nashysolutions
nashysolutions / NetworkMonitor.swift
Created November 21, 2023 10:47
Connectivity monitor using AsyncThrowingStream.
func monitorChanges() async throws {
let stream = makeStream()
for try await status in stream {
self.status = status
}
}
private func makeStream() -> AsyncThrowingStream<Status, Error> {
AsyncThrowingStream { continuation in
let monitor = NWPathMonitor()
@nashysolutions
nashysolutions / ReportsView.swift
Created March 18, 2021 22:46
How to pass a binding from within ForEach
extension ReportStore where Report: Identifiable {
func binding(for report: Report) -> Binding<Report> {
let index = fetchedItems.firstIndex(where: { $0.id == report.id } )!
return Binding(
get: { self.fetchedItems[index] },
set: { self.fetchedItems[index] = $0 }
)
}
}
class MenuViewController: UIViewController {
let table = MenuTableHandler()
var categories: [MenuCategory] = []
var selections = Set<MenuItem>()
fileprivate var searchText: String? {
didSet {
if oldValue == searchText { return }
categories = MenuOption.allCases.map {
extension MenuViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
table.reloadItem(at: indexPath, save: { (item) in
switch selections.contains(item) {
case true: selections.remove(item)
case false: selections.insert(item)
}
}, animate: false)
}
func toggleSelectedStateOfItem(at indexPath: IndexPath) {
let item = dataSource.itemIdentifier(for: indexPath)!
var snapshot = dataSource.snapshot()
item.isSelected = !item.isSelected
snapshot.reloadItems([item])
dataSource.apply(snapshot)
}
let cellProvider: CellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(
extension MenuItem: Hashable, Comparable {
func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}
extension MenuItem: Comparable {
static func <(lhs: MenuItem, rhs: MenuItem) -> Bool {
private typealias CellProvider = (UITableView, IndexPath, MenuItem) -> UITableViewCell?
private let cellProvider: CellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(
withIdentifier: MenuItemTableViewCell.identifier,
for: indexPath) as! MenuItemTableViewCell
cell.accessoryType = item.isSelected ? .checkmark : .none
cell.textLabel?.text = item.name
return cell
}
struct MenuCategory {
let name: String
let description: String?
let items: Set<MenuItem>
}
struct MenuItem {
let name: String
let description: String
let category: MenuCategory