Skip to content

Instantly share code, notes, and snippets.

View AndrewBennet's full-sized avatar

Andrew Bennet AndrewBennet

View GitHub Profile
@AndrewBennet
AndrewBennet / ManagedObjectDeletionFiringFault.swift
Created March 3, 2018 14:20
A Swift playground demonstrating that deleting an NSManagedObject causes a fault to fire
import CoreData
import PlaygroundSupport
// Requires a momd called "TestModel" be added to the Resources directory of the playground.
// This can be obtained by creating a model in an iOS app, building, and copying the momd directory
// from the resulting Products directory into this playground's Resources directory.
// The model has a single entity called "Entity" with a single optional string attribute called "testAttribute".
// There are no relationships in the model.
@AndrewBennet
AndrewBennet / ChildContextInsertion.swift
Created February 26, 2018 08:01
Why does NSManagedObject not get inserted into a child NSManagedObjectContext when the context initialised inline?
import CoreData
import PlaygroundSupport
extension NSManagedObjectContext {
func childContext(concurrencyType: NSManagedObjectContextConcurrencyType = .mainQueueConcurrencyType) -> NSManagedObjectContext {
let childContext = NSManagedObjectContext(concurrencyType: concurrencyType)
childContext.parent = self
return childContext
}
}
@AndrewBennet
AndrewBennet / CSVImporter_Bug.swift
Created January 31, 2018 10:21
"Fatal error: Index out of range" when both ',' and '\n' are within a quoted cell
import XCPlayground
import CSVImporter
class Row {
let cell1: String
let cell2: String
let cell3: String
init(cell1: String, cell2: String, cell3: String){
self.cell1 = cell1
self.cell2 = cell2
@AndrewBennet
AndrewBennet / CompoundFetchedResultsController.swift
Last active March 11, 2018 09:58
CompoundFetchedResultsController: a wrapper of a multiple NSFetchedResultsControllers.
/**
A CompoundFetchedResultsController is a wrapper of a number of inner NSFetchedResultsControllers.
The wrapper flattens the sections produced by the inner controllers, behaving as if all sections
were fetched by a single controller. Additionally, change notifications are mapped before being
passed to the optional NSFetchedResultsControllerDelegate property, so that the section indices
in the notifications reflect the flattened section indicies.
Example use case: a table where sections should be ordered in mutually opposing ways. E.g., if
section 1 should be ordered by propery A ascending, but section 2 should be ordered by property A
descending. In this case, two controllers can be created - one ordering ascending, the other de-