Skip to content

Instantly share code, notes, and snippets.

View andreacipriani's full-sized avatar

Andrea Cipriani andreacipriani

  • New York
View GitHub Profile
import Foundation
// Models:
struct TrackModel {
let title: String
}
struct UserModel {
let name: String
@andreacipriani
andreacipriani / DispatchGroupSwift5Example.swift
Created September 12, 2019 10:14
Dispatch group on Swift example: how to wait for multiple asynchronous closures to complete
let group = DispatchGroup()
group.notify(queue: .main, work: DispatchWorkItem(block: {
print("everything finished")
}))
func asyncOne() {
sleep(2)
group.enter()
print("finished A")
@andreacipriani
andreacipriani / TestAsync.swift
Created April 6, 2020 17:35
Show the problem of testing an async function in Tuist
class Subject {
let dependency: Dependency
func doSomethingAsync() {
DispatchQueue.main.async {
dependency.doSomething()
}
}
}
@andreacipriani
andreacipriani / UIImagePickerControllerWithCircleOverlay.c
Last active April 16, 2020 14:42
iOS: UIImagePickerController editing view circle overlay
/**
Credit: I've started by reading this SO question: http://stackoverflow.com/questions/20794187/uiimagepickercontroller-editing-view-circle-overlay-edited
Trick to add a circle view on image picker editing to facilitate circular cropping
Tested on iPhone4s, iPhone5, iPhone6, iPhone6+, iPad - iOS 7 and iOS 8 - on May 2015
**/
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
@andreacipriani
andreacipriani / CombineAsyncMapping.swift
Last active March 24, 2021 19:26
An example that shows how to map a publisher with an asynchronous function, using DispatchGroup
func makeUppercase(_ string: String, completion: (String) -> Void) {
sleep(2)
completion(string.uppercased())
}
let subject = CurrentValueSubject<String, Never>("foo")
let publisher = subject.eraseToAnyPublisher()
let group = DispatchGroup()
@andreacipriani
andreacipriani / CombineAsyncProblem.swift
Created April 3, 2021 20:16
Combine async publisher problem
import UIKit
import Combine
let queue = DispatchQueue(label: "bg", qos: .background)
let asyncOpQueue = DispatchQueue(label: "async-op-queue")
var cancellable: AnyCancellable?
func example() {
let source = PassthroughSubject<Bool, Never>()
cancellable = source
@andreacipriani
andreacipriani / cloudSettings
Created December 3, 2021 21:46
Visual Studio Code Settings Sync Gist
{"lastUpload":"2021-12-03T21:46:55.902Z","extensionVersion":"v3.4.3"}
@andreacipriani
andreacipriani / DecorateGraph.swift
Created March 8, 2022 17:40
Given a DAG with raw edges, resolve the edges to be "type" nodes
import Foundation
struct RawNode {
let name: String
let value: Int
let rawDependencies: [String]
}
// Given a DAG of RawNodes uniquely identified by a `name`
@andreacipriani
andreacipriani / Bash.swift
Last active February 15, 2024 12:50
Execute shell/bash commands from Swift
import UIKit
protocol CommandExecuting {
func run(commandName: String, arguments: [String]) throws -> String
}
enum BashError: Error {
case commandNotFound(name: String)
}