Created
April 6, 2015 18:13
-
-
Save derrh/c9b8d13377eda7995391 to your computer and use it in GitHub Desktop.
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
// | |
// NSManagedObjectContext+Project.swift | |
// | |
// Created by Derrick Hathaway on 2/22/15. | |
// Copyright (c) 2015 The Best Bits LLC. | |
// | |
// Permission is hereby granted, free of charge, to any person | |
// obtaining a copy of this software and associated documentation | |
// files (the "Software"), to deal in the Software without | |
// restriction, including without limitation the rights to use, | |
// copy, modify, merge, publish, distribute, sublicense, and/or | |
// sell copies of the Software, and to permit persons to whom the | |
// Software is furnished to do so, subject to the following | |
// conditions: | |
// The above copyright notice and this permission notice shall be | |
// included in all copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
// OTHER DEALINGS IN THE SOFTWARE. | |
import CoreData | |
private struct Singleton { | |
static var psc: NSPersistentStoreCoordinator! | |
static var main: NSManagedObjectContext! | |
} | |
private var once: dispatch_once_t = 0 | |
private func prep() { | |
dispatch_once(&once) { | |
let bundle = NSBundle(forClass: OneOfYourModelClasses.classForCoder()) | |
let modelURL = bundle.URLForResource(XCDataModelFileName, withExtension: "momd")! | |
let model = NSManagedObjectModel(contentsOfURL: modelURL) | |
Singleton.psc = NSPersistentStoreCoordinator(managedObjectModel: model!) | |
let libURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.LibraryDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)[0] as NSURL | |
let storeURL = libURL.URLByAppendingPathComponent(file_name.sqlite) | |
var error: NSError? | |
Singleton.psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error) | |
Singleton.main = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) | |
Singleton.main.persistentStoreCoordinator = Singleton.psc | |
} | |
} | |
public extension NSManagedObjectContext { | |
public class var main: NSManagedObjectContext { | |
prep() | |
return Singleton.main | |
} | |
} | |
public typealias MOC = NSManagedObjectContext | |
public extension String { | |
public var ascending: NSSortDescriptor { | |
return NSSortDescriptor(key: self, ascending: true) | |
} | |
public var descending: NSSortDescriptor { | |
return NSSortDescriptor(key: self, ascending: false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment