Skip to content

Instantly share code, notes, and snippets.

@ennioma ennioma/relam_storage_context.swift Secret
Last active Jul 6, 2017

Embed
What would you like to do?
/* 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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.