Skip to content

Instantly share code, notes, and snippets.

@tonyarnold
Last active January 18, 2018 00:31
Show Gist options
  • Save tonyarnold/e4b614f75434c6092c7e to your computer and use it in GitHub Desktop.
Save tonyarnold/e4b614f75434c6092c7e to your computer and use it in GitHub Desktop.
//
// Copyright © 2015 The CocoaBots. All rights reserved.
import Cocoa
import RealmSwift
enum RealmDocumentError: ErrorType {
case FailedToConvertURLToFilePath
}
class RealmDocument: NSDocument {
var representedRealm = Realm(inMemoryIdentifier: NSUUID().UUIDString)
convenience init(contentsOfURL url: NSURL, ofType typeName: String) throws {
self.init()
do {
try representedRealm = Realm(url: url)
} catch {
fatalError("Yeeeeeah, something went wrong creating the realm for \(url)")
}
}
convenience init(forURL urlOrNil: NSURL?, withContentsOfURL contentsURL: NSURL, ofType typeName: String) throws {
self.init()
let originalRealm = try Realm(url: contentsURL, readOnly: true)
if let url = urlOrNil, let path = url.path {
try originalRealm.writeCopyToPath(path)
representedRealm = try Realm(path: path)
} else {
let temporaryInMemoryRealmPath = NSTemporaryDirectory().stringByAppendingPathComponent(NSUUID().UUIDString)
try originalRealm.writeCopyToPath(temporaryInMemoryRealmPath)
representedRealm = try Realm(path: temporaryInMemoryRealmPath)
}
}
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
if representedRealm.path.hasPrefix(NSTemporaryDirectory()) {
do {
let fileManager = NSFileManager.defaultManager()
try fileManager.removeItemAtPath(representedRealm.path)
} catch let error as NSError {
print("Error while deleting temporary Realm: \(error.localizedDescription)")
}
}
}
override func readFromURL(url: NSURL, ofType typeName: String) throws {
if let proposedRealmPath = url.path {
let currentRealmPath = representedRealm.path
if currentRealmPath != proposedRealmPath {
try representedRealm.writeCopyToPath(proposedRealmPath)
representedRealm = try Realm(path: proposedRealmPath)
let fileManager = NSFileManager.defaultManager()
try fileManager.removeItemAtPath(currentRealmPath)
}
} else {
throw RealmDocumentError.FailedToConvertURLToFilePath
}
}
override func writeToURL(url: NSURL, ofType typeName: String) throws {
if let filePath = url.path {
try representedRealm.writeCopyToPath(filePath)
} else {
throw RealmDocumentError.FailedToConvertURLToFilePath
}
}
override var documentEdited: Bool {
return representedRealm.inWriteTransaction
}
}
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