Created
April 11, 2019 10:30
-
-
Save nil-biribiri/c469246766713b9ca385f54a4ab4561c to your computer and use it in GitHub Desktop.
Realm manager for Swift 5.0
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
import Realm | |
import RealmSwift | |
class RealmManager { | |
static let shared = RealmManager() | |
private func getRealm() -> Realm { | |
if let _ = NSClassFromString("XCTest") { | |
return try! Realm(configuration: Realm.Configuration(fileURL: nil, inMemoryIdentifier: "test", encryptionKey: nil, readOnly: false, schemaVersion: 0, migrationBlock: nil, objectTypes: nil)) | |
} else { | |
return try! Realm(); | |
} | |
} | |
func objects<T: Object>(_ type: T.Type, predicate: NSPredicate? = nil) -> Results<T>? { | |
if !isRealmAccessible() { return nil } | |
let realm = getRealm() | |
realm.refresh() | |
return predicate == nil ? realm.objects(type) : realm.objects(type).filter(predicate!) | |
} | |
func object<T: Object>(_ type: T.Type, key: Any) -> T? { | |
if !isRealmAccessible() { return nil } | |
let realm = getRealm() | |
realm.refresh() | |
return realm.object(ofType: type, forPrimaryKey: key) | |
} | |
func add<T: Object>(_ data: [T], update: Bool = true) { | |
if !isRealmAccessible() { return } | |
let realm = getRealm() | |
realm.refresh() | |
if realm.isInWriteTransaction { | |
realm.add(data, update: update) | |
} else { | |
try? realm.write { | |
realm.add(data, update: update) | |
} | |
} | |
} | |
func add<T: Object>(_ data: T, update: Bool = true) { | |
add([data], update: update) | |
} | |
func runTransaction(action: () -> Void) { | |
if !isRealmAccessible() { return } | |
let realm = getRealm() | |
realm.refresh() | |
try? realm.write { | |
action() | |
} | |
} | |
func delete<T: Object>(_ data: [T]) { | |
let realm = getRealm() | |
realm.refresh() | |
try? realm.write { realm.delete(data) } | |
} | |
func delete<T: Object>(_ data: T) { | |
delete([data]) | |
} | |
func clearAllData() { | |
if !isRealmAccessible() { return } | |
let realm = getRealm() | |
realm.refresh() | |
try? realm.write { realm.deleteAll() } | |
} | |
} | |
extension RealmManager { | |
func isRealmAccessible() -> Bool { | |
do { _ = try Realm() } catch { | |
print("Realm is not accessible") | |
return false | |
} | |
return true | |
} | |
func configureRealm() { | |
let config = RLMRealmConfiguration.default() | |
config.deleteRealmIfMigrationNeeded = true | |
RLMRealmConfiguration.setDefault(config) | |
} | |
} |
@SlavchoPetkovski You'd want to do something along the lines of
do {
try RealmManager.shared.getRealm().write({
})
} catch {
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So how do you update an object with this manager?
If you do that and call
add
method the app will crash because the object is persisted by Realm and can't be updated outside of a write transaction.Should I use the
runTransaction
method?