/relam_storage_context.swift Secret
Last active
July 6, 2017 21:33
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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