Skip to content

Instantly share code, notes, and snippets.

View mwrites's full-sized avatar
🔧

Supermercat mwrites

🔧
View GitHub Profile
@mwrites
mwrites / JobQueueCenter.swift
Last active September 12, 2017 14:39
JobQueue implementation in Swift
//MARK: -
//MARK: Private
class JobQueueCenter {}
//MARK: -
//MARK: Public API
extension JobQueueCenter {
static let current = JobQueueCenter()
@mwrites
mwrites / Job.swift
Last active September 14, 2017 03:07
JobQueueCenter Job
protocol Job : NSCoding {
var retryableCount: Int { get }
func run()
}
@mwrites
mwrites / JobQueueCenter.swift
Last active June 2, 2019 12:49
JobQueue Implementation in Swift
//MARK: -
//MARK: Private
class JobQueueCenter {
fileprivate let storageAccessQueue = DispatchQueue(label: "com.JobQueueCenter.storageAccessQueue")
fileprivate lazy var storage: JobQueueStorage = { ... }
fileprivate func serializeStorage() {}
fileprivate init() {
NotificationCenter.default.addObserver(self, selector: #selector(tryPersist), name: .UIApplicationDidReceiveMemoryWarning, object: nil)
@mwrites
mwrites / JobQueueStorage.swift
Created September 12, 2017 14:59
JobQueue Implementation in Swift
protocol JobQueueStorage : NSCoding {
func enqueue(_ element: Job)
func dequeue()-> Job?
}
@mwrites
mwrites / DocumentUploadService.swift
Last active September 14, 2017 13:52
JobQueueCenter Dummy upload service
class DocumentUploadService : NSObject {
var document: Data
init(document: Data) {
self.document = document
super.init()
}
func execute(complete: @escaping (Bool)->Void) {
//do something with the document argument
@mwrites
mwrites / ViewController.swift
Last active September 14, 2017 13:47
JobQueueCenter DocumentUploadService Usage
func upload(fileToUpload: Data) {
//this
DocumentUploadService(document: fileToUpload).execute {
...
}
//becomes that
let job = DocumentUploadService(document: fileToUpload)
JobQueueCenter.current.enqueue(job: job)
}
@mwrites
mwrites / DocumentUploadServiceJob.swift
Created September 14, 2017 11:25
JobQueueCenter DocumentUploadServiceJob
class DocumentUploadService : NSObject, Job {
//MARK: -
//MARK: Logic
var document: Data
init(document: Data) { ... }
func execute(complete: @escaping (Bool)->Void) { ... }
//MARK: -
@mwrites
mwrites / .gitignore
Created October 6, 2017 08:09
Xcode9 .gitignore
# OS X
.DS_Store
# Xcode
xcuserdata/
*.xccheckout
# AppCode
.idea/
@mwrites
mwrites / TextSize.swift
Created October 8, 2017 09:29 — forked from gnou/TextSize.swift
Calculate height of some text when width is fixed
public struct TextSize {
fileprivate struct CacheEntry: Hashable {
let text: String
let font: UIFont
let width: CGFloat
let insets: UIEdgeInsets
fileprivate var hashValue: Int {
return text.hashValue ^ Int(width) ^ Int(insets.top) ^ Int(insets.left) ^ Int(insets.bottom) ^ Int(insets.right)
@mwrites
mwrites / cherry-pick-single-file.md
Created October 11, 2017 05:38
cherry-pick single file

up vote 387 down vote accepted I'd do it with cherry-pick -n (--no-commit) which lets you inspect (and modify) the result before committing:

git cherry-pick -n

unstage modifications you don't want to keep, and remove the

modifications from the work tree as well.