Skip to content

Instantly share code, notes, and snippets.

@kean
kean / DFCoreDataMigration.h
Last active June 18, 2016 20:30
DFCoreDataMigration.h
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Grebenyuk (github.com/kean).
#import <CoreData/CoreData.h>
extern NSString *__nonnull const DFMigrationErrorDomain;
typedef NS_ENUM(NSInteger, DFMigrationError) {
DFMigrationErrorSourceModelNotFound = 1,
@kean
kean / cd_progressive_migration.m
Last active June 19, 2016 09:42
Tricking Core Data Into Performing Progressive Migrations
+ (BOOL)migrateStoreAtURL:(NSURL *)storeURL
models:(NSArray<NSManagedObjectModel *> *)moms // @[ mom_v1, mom_v2, mom_v3 ... ];
error:(NSError **)error {
// Find an index of the current store's mom
NSDictionary *meta = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:error];
if (!meta) {
return NO;
}
NSInteger idx = [moms indexOfObjectPassingTest:^BOOL(NSManagedObjectModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
return [obj isConfiguration:nil compatibleWithStoreMetadata:meta];
@kean
kean / DFCoreDataMigration.m
Last active June 20, 2016 20:50
DFCoreDataMigration.m
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Grebenyuk (github.com/kean).
#import "DFCoreDataMigration.h"
NSString *const DFMigrationErrorDomain = @"DFMigrationErrorDomain";
@implementation DFMigrationManager
@kean
kean / CoreImageHelpers.swift
Created August 22, 2016 14:57
CoreImageHelpers
// MARK - CoreImage
private let sharedContext = CIContext(options: [kCIContextPriorityRequestLow: true])
/// Core Image helper methods.
public extension UIImage {
/// Applies closure with a filter to the image.
///
// Performance considerations. Chaining multiple CIFilter objects is much
/// more efficient then using ProcessorComposition to combine multiple
@kean
kean / Nuke.podspec
Last active September 16, 2016 08:01
Nuke 4.0-beta3 iOS 8
Pod::Spec.new do |s|
s.name = 'Nuke'
s.version = '4.0-beta3'
s.summary = 'A powerful image loading and caching framework'
s.description = <<-EOS
A powerful image loading and caching framework.
Has full featured UI extensions, support for image filters, optional Alamofire and FLAnimatedImage plugins and [more](https://github.com/kean/Nuke).
EOS
@kean
kean / CancellationToken.swift
Last active December 17, 2017 14:52
Cancellation Token in Swift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
/// Manages cancellation tokens and signals them when cancellation is requested.
///
/// All `CancellationTokenSource` methods are thread safe.
public final class CancellationTokenSource {
@kean
kean / spacers.swift
Last active January 10, 2018 18:12
Stacks and Spacers
public typealias Stack = UIStackView
public extension Stack {
@nonobjc public convenience init(_ views: UIView..., with: (UIStackView) -> Void = { _ in }) {
self.init(arrangedSubviews: views)
with(self)
}
@nonobjc public convenience init(_ views: [UIView], axis: UILayoutConstraintAxis = .horizontal, spacing: CGFloat = 0, alignment: UIStackViewAlignment = .fill, distribution: UIStackViewDistribution = .fill) {
self.init(arrangedSubviews: views)
@kean
kean / CancellationToken.swift
Last active November 15, 2018 19:23
CancellationToken
// MARK: - CancellationTokenSource
/// Manages cancellation tokens and signals them when cancellation is requested.
///
/// All `CancellationTokenSource` methods are thread safe.
final class CancellationTokenSource {
/// Returns `true` if cancellation has been requested.
var isCancelling: Bool {
_lock.lock(); defer { _lock.unlock() }
return _observers == nil
@kean
kean / books.md
Last active November 19, 2018 11:47
recommend_to_a_friend

books that I would recommend to a friend (not in any particular order, they are all on different topics):

  • Pro Git by Scott Chacon
  • The Art of Concurrency by Clay Breshears
  • Algorithm Design by Jon Kleinberg , Eva Tardos
  • Structured Computer Organization by Andrew S. Tanenbaum
  • Structure and Interpretation of Computer Programs by H. Abelson and G.J. Sussman
  • The C Programming Language by Brian Kernighan, Dennis Ritchie
  • Code: The Hidden Language of Computer Hardware and Software by Charles Petzold
  • The Pragmatic Programmer: From Journeyman to Master by Andrew Hunt
@kean
kean / AsyncAwaitTest.swift
Last active November 24, 2018 22:47
Fake (blocking) Async/Await for https://github.com/kean/FutureX
class AsyncAwaitTests: XCTestCase {
func testAsyncAwait() {
XCTAssertEqual(fakeAsyncAwait().wait().value, 3)
}
}
func fakeAsyncAwait() -> Future<Int, Error> {
return Future.async {
// Add delay to demonstrate that `await` for `Future<_, Never>` don't