Created
November 14, 2016 21:26
-
-
Save colasbd/3dc4603049c0d66cea44d832530e1b77 to your computer and use it in GitHub Desktop.
NSDocument with Realm persistence
This file contains hidden or 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
// | |
// Copyright © 2015 The CocoaBots. All rights reserved. | |
import Cocoa | |
import RealmSwift | |
enum RealmDocumentError: Error { | |
case FailedToConvertURLToFilePath | |
} | |
class RealmDocument: NSDocument { | |
var representedRealm: Realm = { | |
var config = Realm.Configuration() | |
config.inMemoryIdentifier = NSUUID().uuidString | |
do { | |
return try Realm(configuration:config) | |
} catch { | |
fatalError("Init Realm failed") | |
} | |
}() | |
// private static func realmURL(url: URL) -> URL { | |
// return url.appendingPathComponent(<#T##pathComponent: String##String#>) | |
// } | |
convenience init(contentsOf url: URL, ofType typeName: String) throws { | |
self.init() | |
do { | |
try representedRealm = Realm(fileURL: url) | |
} catch { | |
fatalError("Yeeeeeah, something went wrong creating the realm for \(url)") | |
} | |
} | |
convenience init(for urlOrNil: URL?, withContentsOfURL contentsURL: URL, ofType typeName: String) throws { | |
self.init() | |
var config = Realm.Configuration() | |
config.fileURL = contentsURL | |
config.readOnly = true | |
let originalRealm = try Realm(configuration: config) | |
if let url = urlOrNil { | |
try originalRealm.writeCopy(toFile: url)//originalRealm.writeCopyToPath(path) | |
representedRealm = try Realm(fileURL: url) | |
} else { | |
var temporaryInMemoryRealmURL = URL(fileURLWithPath:NSTemporaryDirectory()) | |
temporaryInMemoryRealmURL.appendPathComponent(NSUUID().uuidString) | |
try originalRealm.writeCopy(toFile: temporaryInMemoryRealmURL) | |
representedRealm = try Realm(fileURL: temporaryInMemoryRealmURL) | |
} | |
} | |
deinit { | |
// This is a workaround — ideally Realm would allow initialising an in-memory Realm based on an on-disk representation and this cleanup code would be unnecessary | |
guard let url = representedRealm.configuration.fileURL else { | |
return | |
} | |
if url.path.hasPrefix(NSTemporaryDirectory()) { | |
do { | |
let fileManager = FileManager.default | |
try fileManager.removeItem(at: url) | |
} catch let error as NSError { | |
Swift.print("Error while deleting temporary Realm: \(error.localizedDescription)") | |
} | |
} | |
} | |
override func read(from url: URL, ofType typeName: String) throws { | |
let proposedRealmPath = url.path | |
if let currentRealmPath = representedRealm.configuration.fileURL?.path { | |
if currentRealmPath != proposedRealmPath { | |
try representedRealm.writeCopy(toFile: url) | |
var config = Realm.Configuration() | |
config.fileURL = url | |
representedRealm = try Realm(configuration: config) | |
let fileManager = FileManager.default | |
try fileManager.removeItem(atPath: currentRealmPath) | |
} | |
} else { | |
throw RealmDocumentError.FailedToConvertURLToFilePath | |
} | |
} | |
override func write(to url: URL, ofType typeName: String) throws { | |
try representedRealm.writeCopy(toFile: url) | |
} | |
override var isDocumentEdited: Bool { | |
return representedRealm.isInWriteTransaction | |
} | |
} | |
//extension Realm { | |
// | |
// convenience init(url: NSURL, readOnly: Bool = false) throws { | |
// try self.init(path: url.path!, readOnly: readOnly) | |
// } | |
// | |
// var url: NSURL { | |
// return NSURL(fileURLWithPath: path, isDirectory: false) | |
// } | |
// | |
//} | |
//extension NSFileManager { | |
// | |
// func safelyCopyItemAtURL(srcURL:NSURL, toURL:NSURL, overwriteIfExists:Bool = false) throws { | |
// if overwriteIfExists { | |
// try trashItemAtURL(toURL, resultingItemURL: nil) | |
// } | |
// | |
// try copyItemAtURL(srcURL, toURL: toURL) | |
// } | |
// | |
// func safelyMoveItemAtURL(srcURL:NSURL, toURL:NSURL, overwriteIfExists:Bool = false) throws { | |
// if overwriteIfExists { | |
// try trashItemAtURL(toURL, resultingItemURL: nil) | |
// } | |
// | |
// try moveItemAtURL(srcURL, toURL: toURL) | |
// } | |
// | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment