Today’s 0.9 release is not just a step closer to 1.0, but also an important milestone for us: for the first time, all Swift binding source code is available on GitHub. We’ve also improved performance (again) and added useful features like type converters.
ObjectBox’ lightning-fast database offers a cross-platform C API for language-specific bindings. These bindings provide a thin and nimble wrapper around the C calls and expose ObjectBox functionality in the way that feels most natural to the host language (i.e. Swift).
An open-source binding allows you to make changes to ObjectBox and build it on your own, backport it to older OS or Swift versions, and contribute your changes back to us if you feel so inclined.
Is this the binding I’ve been using all this time?
Although it’s one of our newer language bindings, our Swift binding already has a storied and colorful history. The first Swift binding actually predates our C library and was built directly against the core C++ codebase. As such, it used Objective-C to bridge Swift and C++.
Once we had the C library, we set about eliminating the intermediary and started rewriting the central Objective-C portions as pure Swift on top of the C API. The immediate result when we released ObjectBox for Swift 0.8, was a significant speed-up in write operations due to Swift’s new native UTF-8 strings, and struct support.
But for the open source release, we completed this rewrite, and ObjectBox Swift got even faster:
Not having to bridge to Objective-C meant we could take advantage of Swift’s associated types to resolve class/entity associations instead of a runtime look-up, and eliminate a few duplicated classes necessitated by adding Swift features to Objective-C classes. This in turn allowed removing some unnecessary data copying, which translated directly to increased speed.
While we enjoy a good performance increase as much as the next database developer, we also added new features and fixes, like more query comparison operators for scalar types, and improved behavior when adding properties to an entity.
Property Converters
One major new feature of this release is support for converting simple user-defined types into types the database understands. For example, you can mark any RawRepresentable enum with an annotation to tell ObjectBox how to serialize it. Let’s say we have an enum like
1
2
3
4
5
6
enumOlympicRanking:Int{
caseunset=0
casefirst=100
casesecond=200
casethird=300
}
In your class, you simply add the following annotation:
But of course we also let you work with your own types, or system types. Let’s say you wanted to save a window’s dimensions into your database. You do this using a converter class that implements convert() methods that convert your custom type into one of ObjectBox’s built-in types, and back:
1
2
3
4
5
6
7
8
9
classRectConverter{
staticfunc convert(_box:CGRect)->String{
returnNSCoder.string(for:box)
}
staticfunc convert(_boxString:String?)->CGRect{
guard let boxString=boxStringelse{return.zero}
returnNSCoder.cgRect(for:boxString)
}
}
And then you tell ObjectBox to use that converter using an annotation:
And that’s just a selection of the changes you’ll find in the new open-source release. It contains the entire binding, but also the fork of Sourcery that does its duties as our code generator, and a performance test app. It is faster, more feature-full, and we’ve also squashed some bugs in the binding.
To take a look and get started, simply go to our ObjectBox Swift Git repository, where, in addition to our example application, you can now also find the binding’s source code. Or pull version 0.9 via CocoaPods.
We can’t wait for you to try it out and let us know what you think. Don’t forget to star us if you like it!
ObjectBox is a fast object-oriented database for IoT and Mobile edge computing. With its Go support, ObjectBox is a perfect match for Raspberry Pi and will add instant speed to your next Raspberry project.
To get started right away, jump straight to the Installation section.
Local data persistence
In general, databases are great to add data persistence to your project. If you need to turn off the device or if there is a power outage, your sensor statistics or other interesting information are safe. The next time you boot up the application it’s there again as if nothing happened. Another rather obvious advantage is speed and independence from an Internet connection. Storing data on a local drive is just faster than sending it to the cloud and back. And when you store your data locally, your application works with or without an Internet connection. This also means, you have full control over all your data, especially regarding who shall be able to access it… Finally, this reduces cloud costs.
For many databases (e.g. SQLite, PostgreSQL or MySQL), you need to know SQL. And for managing complex datasets, you need to be really good at it. Some like it, but for many developers this is annoying. Also, you might worry about SQL injection attacks.
Advantages of ObjectBox
ObjectBox is a database uniquely designed to sit on IoT and Mobile devices and fix these issues.
Firstly, you do not formulate your queries in SQL. ObjectBox comes with easy APIs allowing you to use the constructs and syntax native to the language you are using anyway. In this way ObjectBox is a NoSQL database. Secondly, benchmarks show that ObjectBox is faster than alternatives. So, even if you’d like to insert the data of a lot of sensors simultaneously, you don’t need to worry about speed anymore. This has the nice side effect that more speed means less CPU and thus is resourceful in every respect. Last not least, because ObjectBox is designed for small devices, it just needs about 1 MB of disk space. If you need something smaller, you can check out the ObjectBox client for Azure Sphere.
If you want to use ObjectBox in your next Raspberry Pi project, here is how you get started:
Installation
We’ll talk about the Go version of ObjectBox here. There are bindings for C, Java and Swift as well – and we will publish articles in the future about them too.
First, you need to have access to the shell (i.e. command line) of your Raspberry Pi. It does not matter if you’re connected via SSH or directly, just make sure that your Raspberry Pi has a working internet connection. Then, just execute the following command to get started right away:
This creates a folder named go in your home directory with the following subdirectories:
go with Go 1.12.7 installation, unless you already had Go 1.12+ installed
objectbox with the shell script update-objectbox.sh you can execute to easily update ObjectBox upon a new release
projects/objectbox-go-test mainly with the file main.go which contains a demo application, with an entity model in its own directory.
Note: The script will install libobjectbox.so globally (confirm by typing “y”), so it might prompt you to enter your root password.
1
2
3
cd~/projects/objectbox-go-test
go build
./objectbox-go-test
Working with ObjectBox Go
The first tool you will come across is objectbox-gogen. It generates the correct Go files out of a Go structure to make it usable with ObjectBox. Assuming your main.go looks something like this:
As long as you only change the program’s logic, you don’t need to do anything. If you change the model, simply execute go genereate ./... to update the auto-generated files for the ObjectBox model interface. Afterwards, you can run your program using go run . .
As a result, you’ll get a directory called objectbox with the files data.mdb and lock.mdb; these are the underlying database files and they will be reused when you execute the program again.
Summary
So, that’s basically it! Setting up ObjectBox and using ObjectBox is easy. Check it out yourself and please share your thoughts with us; we appreciate any feedback. 🙂
Until we release the next part of this tutorial discussing the features of ObjectBox in Go more thoroughly, feel free to check out the fully-fledged demo application in its official repository. Also, if you’d like to learn more about all other features of ObjectBox Go, have a look at the official documentation!
If you encounter any problems during this process, please reach out to us. The best way is to ask a question and tag it “ObjectBox” on Stack Overflow. Finally, we would love hearing your thoughts on ObjectBox via Twitter, Facebook, or Mail (contact [at] objectbox . io).
A few weeks ago, we published ObjectBox for Azure Sphere. Today, we are adding IoT sensor data collection to this. It’s a working demonstration that you can run on your machine along with an Azure Sphere board. To jump straight to it, here is the repository.
Forwarding Sensor Data to ObjectBox Database
We added an example to the Azure Sphere ObjectBox repository illustrating how to read IoT sensor data and forward them to an ObjectBox database. For integrating the sensors, the example relies on the Grove Shield library from the manufacturers of Azure Sphere, Seeed Studio. This library makes reading sensor information super simple. Next, the collected sensor data is sent via an ObjectBox REST Client to a device running a ObjectBox database. Here, sensor data can be processed further.
The demo setup comes with a ready-made ObjectBox HTTP server for download. The Azure Sphere client application has to be built from sources as the IP of the server needs to be white-listed here. Please find a step-by-step guide in the repository.
Setup Overview
Have a look at the general application architecture to get the gist of the demo:
Browsing collected Sensor Data
Once you collected sensor information like light intensity, temperature, and humidity, you may want to view it. The most simple option is the “ObjectBox Browser” that comes embedded with the ObjectBox HTTP server.
Let us know what you think
So, of course we are looking forward to your feedback again! Do you have a use case in mind that you want to discuss with us? Also, please feel free to open a new GitHub issue if you run into a problem or ask a question tagged ObjectBox on Stack Overflow. And finally, don’t hesitate to share your thoughts on ObjectBox / Azure Sphere with us via Twitter, Facebook, or Mail (contact [at] objectbox . io).
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok