Skip to content

Instantly share code, notes, and snippets.

@Monntay
Monntay / asyncAwaitInCombine.swift
Last active March 1, 2022 19:52
async await in combine
extension Publisher {
func awaitSink(cancellable: inout Set<AnyCancellable>) async throws -> Output {
return try await withCheckedThrowingContinuation { continuation in
self.sink { completion in
switch completion {
case .finished:
break
import UIKit
import RxCocoa
import RxSwift
extension Reactive where Base: UIImageView {
func image(withDuration: TimeInterval) -> Binder<UIImage?> {
return Binder(base) { imageView, image in
// needed to avoid "Value of type 'UIView' has no member 'image'"
@Monntay
Monntay / ViewControllerMVVM.swift
Created May 5, 2020 13:56
The view controller of the MVVMRx example
import UIKit
import RxCocoa
import RxSwift
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var nextButton: UIButton!
@Monntay
Monntay / ViewModelMVVMRx.swift
Last active May 5, 2020 13:40
ViewModel Example with Rx
import Foundation
import RxSwift
import RxCocoa
class ViewModel {
private let imageRelay: BehaviorRelay<UIImage?> = BehaviorRelay(value: nil)
private let labelTextRelay: BehaviorRelay<String?> = BehaviorRelay(value: nil)
var buttonTapRelay = PublishRelay<Void>()
class MigrationPolicy: NSEntityMigrationPolicy {
override func createDestinationInstances(forSource sInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws {
// MARK: 1 - Check if it is a person
if sInstance.entity.name == "Person" {
// MARK: 2 - fetch the values
let firstName = sInstance.primitiveValue(forKey: "firstname") as? String
let lastName = sInstance.primitiveValue(forKey: "lastname") as? String
let age = sInstance.primitiveValue(forKey: "age") as? String
@Monntay
Monntay / CoreDataMigrationGuideTests_3_4.swift
Last active October 19, 2019 05:59
CoreData Heavy Migration Test 2
// we will test the migration from version 1 to 2. the names of a person changed.
func testHeavyWeightMigration() {
// MARK: 1 read and load the old model
let oldModelURL = Bundle(for: AppDelegate.self).url(forResource: "CoreDataMigrationGuide.momd/CoreDataMigrationGuide 3", withExtension: "mom")!
let oldManagedObjectModel = NSManagedObjectModel(contentsOf: oldModelURL)
XCTAssertNotNil(oldManagedObjectModel)
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: oldManagedObjectModel!)
try! coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
@Monntay
Monntay / CoreDataMigrationGuideTests_2_3.swift
Last active October 19, 2019 05:58
CoreData heavy Migration Test 1
// we will test the migration from version 1 to 2. the names of a person changed.
func testHeavyWeightMigration() {
// MARK: 1 - read and load the old model
let oldModelURL = Bundle(for: AppDelegate.self).url(forResource: "CoreDataMigrationGuide.momd/CoreDataMigrationGuide 2", withExtension: "mom")!
let oldManagedObjectModel = NSManagedObjectModel(contentsOf: oldModelURL)
XCTAssertNotNil(oldManagedObjectModel)
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: oldManagedObjectModel!)
try! coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
@Monntay
Monntay / CoreDataMigrationGuideTests_1_2.swift
Last active October 18, 2019 14:41
CoreData lightweight Migration test
// we will test the migration from version 1 to 2. the names of a person changed.
func testLightWeightMigration() {
// MARK: 1 - read and load the old model
let oldModelURL = Bundle(for: AppDelegate.self).url(forResource: "CoreDataMigrationGuide.momd/CoreDataMigrationGuide", withExtension: "mom")!
let oldManagedObjectModel = NSManagedObjectModel(contentsOf: oldModelURL)
XCTAssertNotNil(oldManagedObjectModel)
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: oldManagedObjectModel!)
try! coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)