Skip to content

Instantly share code, notes, and snippets.

@ennioma
Last active July 6, 2017 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ennioma/3bffdc093cbd82f877a898f83ed24349 to your computer and use it in GitHub Desktop.
Save ennioma/3bffdc093cbd82f877a898f83ed24349 to your computer and use it in GitHub Desktop.
/* Storage config options */
public enum ConfigurationType {
case basic(url: String?)
case inMemory(identifier: String?)
var associated: String? {
get {
switch self {
case .basic(let url): return url
case .inMemory(let identifier): return identifier
}
}
}
}
class RealmStorageContext: StorageContext {
var realm: Realm?
required init(configuration: ConfigurationType = .basic(url: nil)) throws {
var rmConfig = Realm.Configuration()
rmConfig.readOnly = true
switch configuration {
case .basic:
rmConfig = Realm.Configuration.defaultConfiguration
if let url = configuration.associated {
rmConfig.fileURL = NSURL(string: url) as URL?
}
case .inMemory:
rmConfig = Realm.Configuration()
if let identifier = configuration.associated {
rmConfig.inMemoryIdentifier = identifier
} else {
throw RealmStorageError.inMemoryIdentifierMissing
}
}
try self.realm = Realm(configuration: rmConfig)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment