Created
January 24, 2015 23:08
-
-
Save drewcrawford/ebd7045bff7db6e4571e to your computer and use it in GitHub Desktop.
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
// | |
// 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