If you’ve ever needed a database for your iOS app, you’ve probably had to manage schemas, tables, query strings and all sorts of overhead. Moreover, whenever you wanted to modify the structure of your database, you had to write migration code so that your users’ data would be upgraded to fit the new structure.

Wouldn’t it be nice if your database just did all that for you, automatically? That was what we thought when we designed ObjectBox. ObjectBox thinks the way a Swift developer does: You take your objects and stick them in a box. You use regular Swift methods and operators to search for objects in your database. You add or remove fields and the database just copes with it. And you can use it right now.

What’s New in the Beta? If you’ve been using the Alpha, here’s what’s new for you: Transparent model migration

Indexed and unique fields

Better support for projects not using CocoaPods

See the Release Notes for more detail

The Code Generator

To do all that work for you, ObjectBox looks at the classes you want it to store. The first time it sees them, it assigns invisible, unique ID numbers to them. And it generates a bit of boilerplate code for you. Don’t worry, the code is safely tucked away in categories in its own file, you do not need to edit it, you do not need to merge it — you just take advantage of it.

Whenever you build your project, ObjectBox reads your code again, and compares it to its previous state. If you add a property, remove one, it makes a note of that. It transparently keeps your database model in sync with your code*.

Searching

Most databases make you use SQL: you build a string of text containing a query expression, maybe you compile it, and only then can you use it to actually search through your objects. Moreover, if you mistype a property’s name or forget to escape special characters like quotes in your string-building code, things won’t go wrong until run-time.

ObjectBox uses regular Swift expressions for queries:

let maybeSteveJobs = personBox.query({ Person.firstName.startsWith("Ste") && Person.lastName.startsWith("Jo") }).find() 1 2 3 4 let maybeSteveJobs = personBox . query ( { Person . firstName . startsWith ( "Ste" ) && Person . lastName . startsWith ( "Jo" ) } ) . find ( )

No extra escaping. You just do what you’ve been doing all day long in Swift. And the Swift compiler knows all your property and class names and will complain about any typos.

Indexes

ObjectBox is fast. And with a few hints, you can make searches faster: if you know that you will be searching for an object’s name, you can add an index annotation:

class MyClass: Entity { var id: Id<MyClass> = 0 // objectbox: index var username = "" init() {} } 1 2 3 4 5 6 class MyClass : Entity { var id : Id < MyClass > = 0 // objectbox: index var username = "" init ( ) { } }

This will tell ObjectBox to build an index over the object list, and allow ObjectBox to more quickly skip objects that don’t match your search terms.

For String properties, ObjectBox indexes are optimized even more: ObjectBox calculates a hash for your strings, and builds the index for that. A hash is 4 bytes. Your strings are much longer. Hashing significantly cuts down on the amount of data that has to be compared. Hashes are much more space-efficient, and speed up looking up something based on a full string, however, they won’t help when you’re searching for a prefix. But you can still use an index. Just tell ObjectBox to use the entire value instead of just a hash (as it does for numeric types):

// objectbox: index = value var username = "" 1 2 // objectbox: index = value var username = ""

Most databases have entries that need to be unique. For example, you can’t have two users in a user database with the same login name. Now it would be annoying if you had to explicitly search through the database to see if a username is already taken before you added a new user entry. So ObjectBox takes care of that for you:

// objectbox: unique var username = "" 1 2 // objectbox: unique var username = ""

If you now try to put one object into your database with a username that’s already taken, ObjectBox will throw a Swift error, and you can notify the user.

Try it!

Since this all sounds too easy, why not just try out ObjectBox right now? It is just a pod install away. Or you can get the latest release from GitHub and add it to your project manually, if you’re not a CocoaPods person.

*) Renaming a property or class requires a little bit of a hint: Just write a special comment next to the property or class name indicating you want to rename, build, and you will get an error message containing the internal unique ID. Copy that into the comment, and then rename your property or class. ObjectBox now knows that this is the same property.