Businesses and people rarely come head to head with problems that change very often. Ironically, the solutions are far more volatile. For example, problem of transportation is just getting from point A to B in a timely, reliable way. The solutions: Airplanes, SpaceX, Cars and Trucks (all of them), Containers, Shipment, Tanks, and the list goes on and on. Ever since the dawn of computers, getting, collecting, and saving data has been one of the hardest painpoints for software development. The entire space is littered with databases so diverse and yet so similar that it’s hard to choose one solution.

Designing data structures has long been one of the easiest things to do when writing software. All one needs to do is write a class. However, data persistence is one of the most open ended questions. One only needs to see how hard it is to save and maintain the simplest data structure in iOS. Most database solutions are interfaced using language specific clients, a minimal shell over their command line APIs, often providing an awkward layer of direct usage. Object Relational Mapping’s (ORM) have been a popular solution for bridging the gap between the language’s runtime and the persistence later by using native objects to only represent snapshots of data in the database.

So you can probably see what’s going on here. We’ve been piling solutions on top of half-solutions abstracting things away but not truly solving the issue that the database and the developer’s runtime are two wholly different animals awkwardly duct taped together. Realm is a completely different mode of interacting with a database. Instead of Object Relational Mappings or a low level API, Realm embraces your class and creates objects that directly represent their data on disk.

1
2
3
4
5
6
7
8
9
import RealmSwift
class Car: Object {
dynamic var _id: String = ""
dynamic var name: String = ""
dynamic var mileage: Int = 0
}

That’s it for your definitions.

To write to the database, you don’t even need to leave your language level bindings. Simply call

1
2
3
4
5
6
7
8
9
10
let realm = Realm()
let carToAdd = Car()
carToAdd._id = "2404"
carToAdd.name = "Honda Civic"
carToAdd.mileage = 24542
try! realm.write {
realm.add(carToAdd)
}