Cadmium (2016)

Cadmium was my first attempt at making Core Data feel less painful. The basic premise was: hide managed object contexts entirely, and wrap everything in transactions that look like closures.

Cd.transact {
    try! Cd.objects(Employee.self).fetch().forEach {
        $0.salary += 10000
    }
}

Under the hood there’s the usual context dance — a root save context on a private queue, a read-only main queue child context, multiple writeable background contexts — but the caller never has to think about any of it. Reads happen wherever you are; writes happen inside Cd.transact { ... } blocks on background threads. The framework would aggressively raise exceptions if you tried to mutate something outside a transaction, the idea being that it’s better to crash at the point you make a mistake than to fail silently at save time.

The biggest hole in Cadmium, in retrospect, was that it didn’t really enforce anything about object lifetimes outside of transactions. Nothing stopped you from holding onto a managed object and using it on the wrong thread later. That class of bug is exactly what Slate was built to solve a couple years later, by leaning hard on immutability and codegen.

Source on github.