The swift iOS database – ObjectBox Swift 1.0 Released

Update: newer versions have been released. Check the changelog.

ObjectBox Swift 1.0 is here! Since the first public alpha released 10 months ago, we’ve worked hard and made major changes to put Swift first, tune the performance, and iterate on the API. We hope you love the result and appreciate your feedback.

All of this, to bring you the features you expect from a database, but more importantly – the features that we think delight developers and sets ObjectBox apart from other databases out there. Let’s swiftly (cheap pun intended) dive into ObjectBox Swift 1.0:

Built with Swift in Mind

// objectbox: entity class Olympian { var id: Id = 0 ... } let store = try Store(directoryPath: dbURL.path) let box = store.box(for: Olympian.self) let olympians: [Olympian] = try box.all() let goldMedalist: Olympian = try box.get(sarahHoefflinId) 1 2 3 4 5 6 7 8 9 10 // objectbox: entity class Olympian { var id : Id = 0 . . . } let store = try Store ( directoryPath : dbURL . path ) let box = store . box ( for : Olympian . self ) let olympians : [ Olympian ] = try box . all ( ) let goldMedalist : Olympian = try box . get ( sarahHoefflinId )

ObjectBox isn’t just a database bolted onto Swift. Your database entities are regular Swift classes or structs that you devise. No need to subclass a particular class (as with CoreData’s NSManagedObject), nor to write tedious serialization code. ?

All you need to do is add one property for the unique ID, build your project, and ObjectBox’s code generator will write a little bit of code for you, just like the Swift compiler does for Codable objects. All that’s left then, is to call a simple method like put() on the object to write it out:

try box.put(ruthJebet) 1 try box . put ( ruthJebet )

We’ve tried to keep this simplicity throughout the Swift binding, e.g. making it very easy to use any RawRepresentable enum without writing any conversion code.

Automatic Schema Migrations

A common chore with databases is schema migration. ObjectBox takes care of that. If you add a new property or class there are no additional migration steps required. Old objects will keep working, and new objects will be saved with the additional fields. Similarly, adding new classes will add them to the database without any error-prone migration steps.

Moreover, you do not need to maintain a dedicated schema, because your classes and structs are the schema in the first place.

Relations

class Author: Entity { var id: Id = 0 var name: String var books: ToMany<Book> = nil ... } amandaPalmer.books.append(theArtOfAsking) try amandaPalmer.books.applyToDb() 1 2 3 4 5 6 7 8 9 10 class Author : Entity { var id : Id = 0 var name : String var books : ToMany < Book > = nil . . . } amandaPalmer . books . append ( theArtOfAsking ) try amandaPalmer . books . applyToDb ( )

To ensure ObjectBox knows how to save object references, you use a wrapper class. Either ToOne or ToMany, instead of a straight reference or an array. This lets ObjectBox lazily load the related objects from the database, only when you’re actually accessing a related object.

The Swift 1.0 release brings you our complete set of relations: One-to-many, many-to-many, and their corresponding back-links. ToMany behaves just like any other Swift collection, you can add or remove objects as you please with your familiar methods like append().

Queries

let query = try box.query { Author.fname == "Jeaniene" }.build() let results = try query.find() // All Authors matching query. // All last names of the matching authors: let names = try query.property(Author.lname) .distinct().findStrings() 1 2 3 4 5 let query = try box . query { Author . fname == "Jeaniene" } . build ( ) let results = try query . find ( ) // All Authors matching query. // All last names of the matching authors: let names = try query . property ( Author . lname ) . distinct ( ) . findStrings ( )

Of course ObjectBox lets you perform queries to collect data; either complete objects or individual properties (basic Swift data types).

But with ObjectBox you don’t mess around with query strings or unpack data from cursors. You simply write Swift expressions with function calls and operators you’re already used to.

Also, you get to keep the type-safety guarantees and compile-time checking. So you don’t have to spend hours figuring out why your query doesn’t return the proper results, just to discover you made a typo in a field name in a query string.

subscription = query.subscribe() { authors, error in if let error = error { self.reportError(error); return } self.authors = authors self.tableView.reloadData() } 1 2 3 4 5 subscription = query . subscribe ( ) { authors , error in if let error = error { self . reportError ( error ) ; return } self . authors = authors self . tableView . reloadData ( ) }

ObjectBox lets you then operate on these objects, watch a query for changes, retrieve the results, delete the objects matching a query etc. The source code even contains a file that adds Combine support so you can integrate with its pipelines to take advantage of Apple’s newest technology.

Open Source Swift Binding

If you’re curious how things work behind the scenes, feel free to check out the Swift source code. The source code for the Swift binding, as well as our code generator based on Sourcery, are available among other projects through our Github account.

How-to Get Started

It’s a matter of minutes to get started with ObjectBox. Check our setup instructions (based on CocoaPods) and jump right into code with the getting started guide.

Your Feedback. And what’s Next?

As always, we would love to hear your feedback! Do you like ObjectBox as much as we do? We put our hearts in this product and are excited to learn your thoughts: What features are you most excited about, what are we missing?

We haven’t written much about a topic very dear to us: performance. We will cover this in a follow up post. Also, look forward to our ObjectBox Swift 1.0 benchmarks, which we will release soon including the sources.

——

Looking for a fast and simple data synchronization solution?



