Docs Menu
Docs Home
/ /
Atlas Device SDKs
/

Use Realm in Xcode Playgrounds

On this page

  • Prerequisites
  • Create a Playground
  • Import Realm
  • Experiment with Realm
  • Managing the Realm File in Your Playground

You can only use Swift packages within Xcode projects that have at least one scheme and target. To use Realm in Xcode Playgrounds, you must first have an Xcode project where you have Installed the Swift SDK.

Tip

See also: Other quick starts and tutorials

For more guidance on getting started with Realm and Swift or SwiftUI, see any of these quick starts or tutorials:

Within a project, go to File > New > Playground. Select the type of Playground you want. For this example, we've used a Blank iOS Playground.

Screenshot of Xcode Playground iOS types with Blank selected
click to enlarge

Name and save the playground in the root of your project. Be sure to add it to the project:

Screenshot of the File Navigator with a Playground filename and the project selected
click to enlarge

You should see your new Playground in your Project navigator.

Screenshot of a file named RealmPlayground.playground in the Xcode Project navigator
click to enlarge

Add the following import statement to use Realm in the playground:

import RealmSwift

Experiment with Realm. For this example, we'll:

class Drink: Object {
@Persisted var name = ""
@Persisted var rating = 0
@Persisted var source = ""
@Persisted var drinkType = ""
}
let drink = Drink(value: ["name": "Los Cabellos", "rating": 10, "source": "AeroPress", "drinkType": "Coffee"])
let realm = try! Realm(configuration: config)
try! realm.write {
realm.add(drink)
}
let drinks = realm.objects(Drink.self)
let coffeeDrinks = drinks.where {
$0.drinkType == "Coffee"
}
print(coffeeDrinks.first?.name)

When you work with a default realm in a Playground, you might run into a situation where you need to delete the realm. For example, if you are experimenting with an object type and add properties to the object, you may get an error that you must migrate the realm.

You can specify Realm.configuration details to open the file at a specific path, and delete the realm if it exists at the path.

var config = Realm.Configuration()
config.fileURL!.deleteLastPathComponent()
config.fileURL!.appendPathComponent("playgroundRealm")
config.fileURL!.appendPathExtension("realm")
if Realm.fileExists(for: config) {
try Realm.deleteFiles(for: config)
print("Successfully deleted existing realm at path: \(config.fileURL!)")
} else {
print("No file currently exists at path")
}

Alternately, you can open the realm in-memory only, or use the deleteRealmIfMigrationNeeded method to automatically delete a realm when migration is needed.

Back

Partition-Based Sync

Next

SDK Telemetry