Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Challenges of Building a Database API (in Swift)

The Challenges of Building a Database API (in Swift)

Presented at 360i|Dev 2015.

4d6be90af74894fd132fb06dacec04d7?s=128

Samuel E. Giddins

August 18, 2015
Tweet

More Decks by Samuel E. Giddins

Other Decks in Technology

Transcript

  1. The Challenges of Building a Database API (In Swift) Samuel

    Giddins
  2. Samuel Giddins Cocoa Binding Engineer @ Realm CocoaPods, Bundler, Jazzy,

    RestKit, etc.
  3. Turbocharger for other developers

  4. Realm — Fast, zero-copy, embedded database built from the ground

    up for mobile — Used in apps with hundreds of millions of users — NoSQL (as in, no SQL) — Fully ACID transactions — Well-defined threading model — Cross-platform C++ core with many language bindings (currently Objective-C, Swift, & Android)
  5. None
  6. None
  7. Swift — Strongly typed — Staticly inclined — Objective-C interop

    — Syntactically concise
  8. Databases Defined by a few different questions: — What's the

    atomic unit? — How do you interact with your data? — What's implicit? What's explicit? — What do you optimize for? — Performance — Code — Safety — Concurrency
  9. Databases So much is about the API. Implementing the storage

    engine is hard enough, but it's the easy part.
  10. Databases — Are at the center of almost every mobile

    app. — They're used everywhere in an app. — Perhaps the cornerstone.
  11. Existing Approaches — Core Data — sqlite.h — Redis —

    SQL — ActiveRecord — Sequel — Realm Objective-C
  12. But what about !?

  13. Swift Features — Optionals — Generics & Type Inference —

    Enums — Structs / Value Types — Static Compiler ✨
  14. Realm Objective-C Features — Swizzling — Runtime introspection — Laziness

    — Autoreleasepool-based caching — Native "dynamic" API — On-demand class creation
  15. None
  16. aka Impedence Mismatch

  17. Sacrifices — Type safety — String — AnyObject — Queries

    — Struct value semantics — Non-RawRepresentable enums — Error handling
  18. The Objective-C API we've always wanted

  19. class Book: Object { dynamic var name = "" //

    needs default for schema initialization dynamic var pageCount: Int = 0 dynamic var author: Author? override class func primaryKey() -> String { return "name" } } let realm = Realm() let books = realm.objects(Book).filter("pageCount > 50").sort("name") let book = Book(value: ["name" : "The Name of the Wind"]) book.author = books.filter("name = %@", "The Wise Man's Fear").first?.author book.pageCount = 994 realm.write { realm.add(book) realm.create(Book.self, book, update: true) }
  20. There are lots of beginners.

  21. Approachability — Not everyone is a Haskell wizard — Programmers

    get objects — Boilerplate => !
  22. let realm = Realm() var writeTransaction = realm.beginWrite() let table

    = writeTransaction.table(Book) writeTransaction.set(table, 0, Book.Properties.Name, "Catch-22") realm.commitWrite(writeTransaction)
  23. None
  24. let book = Book() book.name = "Cien Años de Soledad"

    book.pageCount = 324 do { let realm = try Realm() realm.write { realm.add(book, update: true) } } catch { // handle error }
  25. Dynamic — Normally means slow — Slower than static —

    Faster than string-based lookups — Flexible — Can bind to column indices at runtime — Allows for "standalone" objects
  26. Dynamic — Limited to "Objective-C types" — No List<Book> —

    No Int? — No String? — Yes Author? — Requires var — Can be lying to the compiler — No way to enforce — Confusing when a property isn't persisted
  27. book.name = book.name + ": Part Deux" => — Load

    book name — Translate to NSString — Append — Store new book name
  28. Underlying operations still hidden

  29. None
  30. Reporting failures — NSErrorPointer — throws — fatalError — Optional<T>

    — throwRealmException
  31. Testing — Handling failure modes — Code that shouldn't compile

    — Using generics — Looks nothing like user code
  32. Months in the making — jp-swift-xcode-beta-7 — jp-swift — tg-swift

    — al-swift — al-al-swift — master
  33. !

  34. ! — CocoaPods — Carthage — Module Maps — Nested

    Frameworks — Xcodes & Swift Versions — Documentation
  35. The Verdict

  36. The Verdict — Nicest Realm API yet — Least cooperative

    language — Relying on interop is !/" — Prototype something else — Eventually
  37. The Verdict What's Missing — Native metaprogramming — Dynamic structs

    — API / ABI / Tooling -- stability / compatibility — C++ interop — Property properties — Threading in the type system
  38. The Verdict Swift isn't ready for this

  39. Yet

  40. Give us one or two more features, and a few

    years, and Swift could give us the perfect platform for building expressive, concise, powerful, & performant APIs.
  41. Samuel Giddins Cocoa @ Realm @segiddins