Skip to content

Instantly share code, notes, and snippets.

@drewcrawford
Created January 24, 2015 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewcrawford/ebd7045bff7db6e4571e to your computer and use it in GitHub Desktop.
Save drewcrawford/ebd7045bff7db6e4571e to your computer and use it in GitHub Desktop.
//
// RLMRealmDescription.swift
// DCAKit
//
// Created by Drew Crawford on 1/24/15.
// Copyright (c) 2015 DrewCrawfordApps. All rights reserved.
//
import Foundation
import Realm
/**A complete, threadsafe, description for a realm. Using the description, the realm can be generated lazily for the correct thread.
*/
public class RealmDescription {
private let realmSpecifier : RealmSpecifier
/**Because in-memory realms are not persisted by default, we pack
a reference to them inside the RealmDescription itself.
In this way, an in-memory realm's lifetime is the lifetime of all its descriptions. */
private var realmReference : RLMRealm? = nil
private let referenceQueue : dispatch_queue_t = dispatch_queue_create("RealmDescriptionReferenceQueue",DISPATCH_QUEUE_CONCURRENT)
public enum RealmSpecifier {
case Default
case OnDisk(path: String)
case InMemory(identifier: String)
}
public func getRealm() -> RLMRealm {
switch(self.realmSpecifier) {
case .Default:
return RLMRealm.defaultRealm()
case .OnDisk(let path) :
return RLMRealm(path: path)
case .InMemory(let identifier):
let result = RLMRealm.inMemoryRealmWithIdentifier(identifier)
dispatch_barrier_sync(referenceQueue) {
self.realmReference = result
}
return result
}
}
public init (realmSpecifier: RealmSpecifier) {
self.realmSpecifier = realmSpecifier
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment