Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Last active August 2, 2016 17:16
Show Gist options
  • Save ccabanero/b50349bb9d87de79d93a7e2aa84a3a48 to your computer and use it in GitHub Desktop.
Save ccabanero/b50349bb9d87de79d93a7e2aa84a3a48 to your computer and use it in GitHub Desktop.
realm-notes
Parts of the doc that I don't want to forget ...
# finding a realm file - so you can view in Realm Browser
# link: https://realm.io/docs/swift/latest/#finding-a-realm-file
in lldb ... po Realm.Configuration.defaultConfiguration.fileURL
in terminal open the above path
# seperate realms for different users
# link: https://realm.io/docs/swift/latest/#other-realms
let config = Realm.Configuration(
// Get the URL to the bundled file
fileURL: NSBundle.mainBundle().URLForResource("MyBundledData", withExtension: "realm"),
// Open the file in read-only mode as application bundles are not writeable
readOnly: true)
// Open the Realm with the configuration
let realm = try! Realm(configuration: config)
// Read some data from the bundled Realm
let results = realm.objects(Dog.self).filter("age >
# configuring migrations
# linke: https://realm.io/docs/swift/latest/#migrations
// Inside your application(application:didFinishLaunchingWithOptions:)
let config = Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1,
// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { migration, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()
# error handling
# link: https://realm.io/docs/swift/latest/#error-handling
do {
let realm = try Realm()
} catch let error as NSError {
// handle error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment