/objectbox-swift

ObjectBox Swift - persisting your Swift objects superfast and simple

Primary LanguageSwiftApache License 2.0Apache-2.0

ObjectBox Swift

Version Platform

ObjectBox is a superfast, light-weight object persistence framework. This Swift API seamlessly persists objects on-device for iOS and macOS.

let santa = Person(firstName: "Santa", lastName: "Claus")
try personBox.put(santa)

let query: Query<Person> = personBox.query {
    return (Person.firstName.contains("Santa") || Person.age > 100)
           && Person.lastName.isEqual(to: "Claus") 
}.build()
let oldClauses = query.find()

Want details? Read the guides or check out the API docs.

Adding ObjectBox to your project

CocoaPods is recommended to set up ObjectBox in your project. See the installation docs for alternative setups, or the New to CocoaPods? section below for a quick intro. To install the ObjectBox pod, add the following line to your Podfile:

  pod 'ObjectBox'

Note: use pod 'ObjectBox501' if you are still on Swift 5.0.1.

Then run this to install the ObjectBox framework:

cd /path/to/your/project/folder/ # whatever folder your Podfile is in.
pod install
Pods/ObjectBox/setup.rb myproject.xcodeproj # whatever your Xcode project is named

Note: run Pods/ObjectBox501/setup.rb myproject.xcodeproj if you are still on Swift 5.0.1.

And, don't forget to close the Xcode project (.xcodeproj) and open the workspace (.xcworkspace) instead. Now, you are all set to define your first ObjectBox entities; e.g. check the getting started guide or the example described below.

New to CocoaPods?

CocoaPods is a dependency manager and sets up libraries like ObjectBox in your Xcode project. To install it, run this in a terminal:

sudo gem install cocoapods

In CocoaPods, you keep track of used libraries in a file called "Podfile". If you don't have this file yet, navigate to your Xcode project folder and use CocoaPods to create one:

pod init

CocoaPods troubleshooting

If pod install fails, try updating CocoaPods first:

gem update xcodeproj && gem update cocoapods && pod repo update

Example

In the Example directory, you'll find a "Notes" example app demonstrating ObjectBox's Swift API. The example comes with two apps: one for iOS and one for macOS. The iOS example is a full GUI application, whereas the macOS example runs a few operations and then exits.

To setup the example, use CocoaPods to acquire the framework:

cd Example/
pod install # if that fails, update CocoaPods (see Installation)
Pods/ObjectBox/setup.rb

This will generate a NotesExample.xcworkspace that you can launch to try out ObjectBox.

How can ObjectBox Help You?

We want ObjectBox to be not only the fastest, but also the most Swift-friendly persistence solution.

To do that, we want your feedback: what do you love? What's amiss? Where do you struggle in everyday app development?

We're looking forward to receiving your comments and requests:

Thank you!

Keep in touch

Sign up here for future updates on ObjectBox Swift.

For general news on ObjectBox, check our blog or follow @ObjectBox_io on Twitter.

Background: code generation

ObjectBox Swift generates code at build time for optimal performance at runtime by avoiding reflection etc. This is automatically done for you and should be transparent. Internally, we use a fork of Sourcery for this.

Source code

Source code for ObjectBox's Swift binding can be found in the Source folder.

Migrating from pre 1.0 versions

If you've used the ObjectBox betas, there are a few changes we made for the 1.0 release to improve the experience before we freeze the API for release. The adjustments you'll have to make are:

  1. isEmpty(), count(), all(), sum(), min(), max() etc. are now functions to make it clearer they may perform a database lookup and aren't just properties to read.
  2. putImmutable() is now gone. Just call put() instead, it now returns the IDs like putImmutable used to.
  3. The old Id<> type has been renamed to EntityId<>. There is now a new Id type that does not use generics.
  4. Changes to standalone relations need to be saved explicitly using a call to applyToDb().
  5. Most calls have been revised to now throw errors, so you'll have to add the requisite try statements.
  6. ToMany has been revised to no longer need the containing type as a second generic parameter, so change any ToMany<A, B> in your code to ToMany<A>.

Other languages/bindings

ObjectBox is a multi platform database supporting multiple languages:

  • ObjectBox Java: runs on Android, desktop and servers.
  • ObjectBox Go: great for data-driven tools and server applications.
  • ObjectBox C: native speed with zero copy access to FlatBuffer objects; also enables porting ObjectBox to other languages.
  • ObjectBox Dart: build cross-platform apps using Flutter (early version)

License

All files in this repository are under the Apache 2 license:

Copyright 2018-2019 ObjectBox Ltd. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.