Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #22 from tomco/master
Browse files Browse the repository at this point in the history
Make underlying managed object context of context public accessible
  • Loading branch information
vmartinelli committed Feb 15, 2015
2 parents a10f0d1 + 458ae09 commit c916786
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Examples/iOS/AlecrimCoreDataExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import CoreData
import AlecrimCoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
Expand All @@ -24,6 +25,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele

let masterNavigationController = splitViewController.viewControllers[0] as UINavigationController
let controller = masterNavigationController.topViewController as MasterViewController

println(localSQLiteStoreURLForBundle(NSBundle.mainBundle()))

return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class MasterViewController: UITableViewController {

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let entity = self.fetchedResultsController.entityAtIndexPath(indexPath)
cell.textLabel.text = entity.timeStamp.description
cell.textLabel?.text = entity.timeStamp.description
}

}
Expand Down
2 changes: 1 addition & 1 deletion Source/Shared/AlecrimCoreData/Classes/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import CoreData
public class Context {

private let stack: Stack!
internal let managedObjectContext: NSManagedObjectContext!
public let managedObjectContext: NSManagedObjectContext!

public init?(managedObjectModelName: String? = nil, stackType: StackType = StackType.SQLite) {
if let stack = Stack(managedObjectModelName: managedObjectModelName, stackType: stackType) {
Expand Down
34 changes: 20 additions & 14 deletions Source/Shared/AlecrimCoreData/Classes/Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,9 @@ internal final class Stack {

private class func persistentStoreForSQLiteStoreTypeWithCoordinator(coordinator: NSPersistentStoreCoordinator, managedObjectModelName: String?, bundle: NSBundle) -> NSPersistentStore? {
if let momn = managedObjectModelName {
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)
let applicationSupportDirectoryURL = urls[urls.endIndex - 1] as NSURL

if let bundleIdentifier = bundle.bundleIdentifier {
let localStoreURL = applicationSupportDirectoryURL.URLByAppendingPathComponent(bundleIdentifier, isDirectory: true)

if let localStorePath = localStoreURL.absoluteString {
if let localStoreURL = localSQLiteStoreURLForBundle(bundle) {
if let localStorePath = localStoreURL.path {
let fileManager = NSFileManager.defaultManager()
var error: NSError? = nil

if !fileManager.fileExistsAtPath(localStorePath) {
Expand All @@ -140,15 +135,14 @@ internal final class Stack {
return nil
}
}

let storeFilename = momn.stringByAppendingPathExtension("sqlite")!
let localStoreFileURL = localStoreURL.URLByAppendingPathComponent(storeFilename, isDirectory: false)

return coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: localStoreFileURL, options: nil, error: nil)!
}

let storeFilename = momn.stringByAppendingPathExtension("sqlite")!
let localStoreFileURL = localStoreURL.URLByAppendingPathComponent(storeFilename, isDirectory: false)

return coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: localStoreFileURL, options: nil, error: nil)!
}
}

return nil
}

Expand Down Expand Up @@ -190,3 +184,15 @@ private class StackBackgroundManagedObjectContext: NSManagedObjectContext {
}

}

public func localSQLiteStoreURLForBundle(bundle: NSBundle) -> NSURL? {
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)
let applicationSupportDirectoryURL = urls.last as NSURL

if let bundleIdentifier = bundle.bundleIdentifier {
return applicationSupportDirectoryURL.URLByAppendingPathComponent(bundleIdentifier, isDirectory: true)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright (c) 2014 Alecrim. All rights reserved.
//

import Foundation
import UIKit

// MARK: - UITableView helper methods

Expand Down

0 comments on commit c916786

Please sign in to comment.