ObjectBox Swift 1.0 Released
Tags: swift, ios, database, mobile
Published: 2019-09-25 12:34:20

---
title: ObjectBox Swift 1.0 Released
published: true
tags: swift, ios, database, mobile
canonical_url: https://objectbox.io/objectbox-swift-1-0-release/
---

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](https://objectbox.io/objectbox-swift-binding-open-sourced/), [tune the performance](https://objectbox.io/speed-increase-with-swift/), and iterate on the API. We hope you will love the result. 

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

```swift
// 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:

```swift
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](https://swift.objectbox.io/advanced/custom-types) 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

```swift
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

```swift
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.

```swift
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](https://developer.apple.com/documentation/combine) 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](https://github.com/objectbox/objectbox-swift), as well as our code generator based on Sourcery, are available among other projects through [our Github account](https://github.com/objectbox).

## How-to Get Started
It’s a matter of minutes to get started with ObjectBox. Check our [setup instructions](https://swift.objectbox.io/install) (based on CocoaPods) and jump right into code with the [getting started guide](https://swift.objectbox.io/getting-started).

## Your Feedback. And what’s Next?
As always, [we would love to hear your feedback](https://docs.google.com/forms/d/e/1FAIpQLSd0neiviD0Yal0Tn7921w-XWI2d0ONpLm7TfVKp7OvwW2Tu2A/viewform?usp=sf_link)! 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.