Skip to content

Commit

Permalink
reduced code instantiation when passing contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Dahan committed Mar 14, 2016
1 parent 9c2bb16 commit 04adf36
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 69 deletions.
10 changes: 1 addition & 9 deletions Examples/Graph.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Examples/Graph.xcworkspace/xcshareddata/Graph.xcscmblueprint
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "14FF5CD0A93F3CC14F0E8C5A4FD8ECB4A6DF5909",
"DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : {

},
"DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : {
"03C6D728E054A05A684543B0D508854F060D607A" : 0,
"14FF5CD0A93F3CC14F0E8C5A4FD8ECB4A6DF5909" : 0
},
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "F41C22B4-E7B3-4518-A78B-4DA7005737D5",
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
"03C6D728E054A05A684543B0D508854F060D607A" : "Algorithm\/",
"14FF5CD0A93F3CC14F0E8C5A4FD8ECB4A6DF5909" : "Graph\/"
},
"DVTSourceControlWorkspaceBlueprintNameKey" : "Graph",
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "Examples\/Graph.xcworkspace",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/danieldahan:[email protected]\/CosmicMind\/Algorithm.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "03C6D728E054A05A684543B0D508854F060D607A"
},
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/danieldahan:[email protected]\/CosmicMind\/Graph.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "14FF5CD0A93F3CC14F0E8C5A4FD8ECB4A6DF5909"
}
]
}
2 changes: 1 addition & 1 deletion Graph.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Graph'
s.version = '1.0.5'
s.version = '1.0.6'
s.license = 'BSD'
s.summary = 'An elegant data-driven framework for CoreData in Swift.'
s.homepage = 'http://cosmicmind.io'
Expand Down
3 changes: 2 additions & 1 deletion Graph.xcodeproj/xcshareddata/xcschemes/Graph OSX.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
3 changes: 2 additions & 1 deletion Graph.xcodeproj/xcshareddata/xcschemes/Graph iOS.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
2 changes: 1 addition & 1 deletion Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class Action : NSObject, Comparable {
/**
:name: init
*/
public convenience init(type: String) {
public required convenience init(type: String) {
self.init(object: ManagedAction(type: type))
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Entity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public class Entity : NSObject, Comparable {
/**
:name: init
*/
public convenience init(type: String) {
public required convenience init(type: String) {
self.init(object: ManagedEntity(type: type))
}

Expand Down
18 changes: 9 additions & 9 deletions Sources/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public class Graph : NSObject {
executed when the save operation is completed.
*/
public func asyncSave(completion: ((success: Bool, error: NSError?) -> Void)? = nil) {
if let p: NSManagedObjectContext = context {
if let p: NSManagedObjectContext = Graph.context {
if p.hasChanges {
p.performBlock {
do {
Expand All @@ -171,7 +171,7 @@ public class Graph : NSObject {
executed when the save operation is completed.
*/
public func save(completion: ((success: Bool, error: NSError?) -> Void)? = nil) {
if let p: NSManagedObjectContext = context {
if let p: NSManagedObjectContext = Graph.context {
if p.hasChanges {
p.performBlockAndWait {
do {
Expand Down Expand Up @@ -209,19 +209,19 @@ public class Graph : NSObject {
/**
:name: context
*/
internal var context: NSManagedObjectContext? {
dispatch_once(&GraphPrivateManagedObjectContext.onceToken) { [unowned self] in
internal static var context: NSManagedObjectContext? {
dispatch_once(&GraphPrivateManagedObjectContext.onceToken) {
GraphPrivateManagedObjectContext.managedObjectContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
GraphPrivateManagedObjectContext.managedObjectContext?.undoManager
GraphPrivateManagedObjectContext.managedObjectContext?.persistentStoreCoordinator = self.persistentStoreCoordinator
GraphPrivateManagedObjectContext.managedObjectContext?.persistentStoreCoordinator = Graph.persistentStoreCoordinator
}
return GraphPrivateManagedObjectContext.managedObjectContext
}

//
// :name: managedObjectModel
//
internal var managedObjectModel: NSManagedObjectModel? {
internal static var managedObjectModel: NSManagedObjectModel? {
dispatch_once(&GraphManagedObjectModel.onceToken) {
GraphManagedObjectModel.managedObjectModel = NSManagedObjectModel()

Expand Down Expand Up @@ -507,16 +507,16 @@ public class Graph : NSObject {
//
// :name: persistentStoreCoordinator
//
internal var persistentStoreCoordinator: NSPersistentStoreCoordinator? {
dispatch_once(&GraphPersistentStoreCoordinator.onceToken) { [unowned self] in
internal static var persistentStoreCoordinator: NSPersistentStoreCoordinator? {
dispatch_once(&GraphPersistentStoreCoordinator.onceToken) {
let directory: String = "Graph/default"
File.createDirectory(File.documentDirectoryPath!, name: directory, withIntermediateDirectories: true, attributes: nil) { (success: Bool, error: NSError?) -> Void in
if !success {
if let e: NSError = error {
fatalError(e.localizedDescription)
}
}
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel!)
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: Graph.managedObjectModel!)
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: File.URL(.DocumentDirectory, path: "\(directory)/\(GraphUtility.storeName)"), options: nil)
} catch {
Expand Down
3 changes: 1 addition & 2 deletions Sources/GraphObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ internal class GraphObject : NSManagedObject {
//
internal var worker: NSManagedObjectContext? {
if nil == context {
let g: Graph = Graph()
context = g.context
context = Graph.context
}
return context
}
Expand Down
24 changes: 12 additions & 12 deletions Sources/GraphSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public extension Graph {
nodes[i] = Entity(object: v)
continue
}
} else if let v: ManagedEntity = context!.objectWithID(nodes[i]["node"]! as! NSManagedObjectID) as? ManagedEntity {
} else if let v: ManagedEntity = Graph.context!.objectWithID(nodes[i]["node"]! as! NSManagedObjectID) as? ManagedEntity {
if nil == seen.updateValue(true, forKey: v.id) {
nodes[i] = Entity(object: v)
continue
Expand All @@ -80,7 +80,7 @@ public extension Graph {
if let v: ManagedEntity = $0 as? ManagedEntity {
return Entity(object: v)
}
return Entity(object: context!.objectWithID($0["node"]! as! NSManagedObjectID) as! ManagedEntity)
return Entity(object: Graph.context!.objectWithID($0["node"]! as! NSManagedObjectID) as! ManagedEntity)
} as Array<Entity>
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ public extension Graph {
nodes[i] = Relationship(object: v)
continue
}
} else if let v: ManagedRelationship = context!.objectWithID(nodes[i]["node"]! as! NSManagedObjectID) as? ManagedRelationship {
} else if let v: ManagedRelationship = Graph.context!.objectWithID(nodes[i]["node"]! as! NSManagedObjectID) as? ManagedRelationship {
if nil == seen.updateValue(true, forKey: v.id) {
nodes[i] = Relationship(object: v)
continue
Expand All @@ -134,7 +134,7 @@ public extension Graph {
if let v: ManagedRelationship = $0 as? ManagedRelationship {
return Relationship(object: v)
}
return Relationship(object: context!.objectWithID($0["node"]! as! NSManagedObjectID) as! ManagedRelationship)
return Relationship(object: Graph.context!.objectWithID($0["node"]! as! NSManagedObjectID) as! ManagedRelationship)
} as Array<Relationship>
}
}
Expand Down Expand Up @@ -174,7 +174,7 @@ public extension Graph {
nodes[i] = Action(object: v)
continue
}
} else if let v: ManagedAction = context!.objectWithID(nodes[i]["node"]! as! NSManagedObjectID) as? ManagedAction {
} else if let v: ManagedAction = Graph.context!.objectWithID(nodes[i]["node"]! as! NSManagedObjectID) as? ManagedAction {
if nil == seen.updateValue(true, forKey: v.id) {
nodes[i] = Action(object: v)
continue
Expand All @@ -188,7 +188,7 @@ public extension Graph {
if let v: ManagedAction = $0 as? ManagedAction {
return Action(object: v)
}
return Action(object: context!.objectWithID($0["node"]! as! NSManagedObjectID) as! ManagedAction)
return Action(object: Graph.context!.objectWithID($0["node"]! as! NSManagedObjectID) as! ManagedAction)
} as Array<Action>
}
}
Expand All @@ -200,15 +200,15 @@ public extension Graph {
typesPredicate.append(NSPredicate(format: "type LIKE[cd] %@", v))
}

let entityDescription: NSEntityDescription = NSEntityDescription.entityForName(typeDescriptionName, inManagedObjectContext: context!)!
let entityDescription: NSEntityDescription = NSEntityDescription.entityForName(typeDescriptionName, inManagedObjectContext: Graph.context!)!
let request: NSFetchRequest = NSFetchRequest()
request.entity = entityDescription
request.fetchBatchSize = batchSize
request.fetchOffset = batchOffset
request.predicate = NSCompoundPredicate(orPredicateWithSubpredicates: typesPredicate)
request.sortDescriptors = [NSSortDescriptor(key: "createdDate", ascending: true)]

return try? context!.executeFetchRequest(request)
return try? Graph.context!.executeFetchRequest(request)
}

internal func search(groupDescriptionName: String, groups: Array<String>) -> Array<AnyObject>? {
Expand All @@ -218,7 +218,7 @@ public extension Graph {
groupsPredicate.append(NSPredicate(format: "name LIKE[cd] %@", v))
}

let entityDescription: NSEntityDescription = NSEntityDescription.entityForName(groupDescriptionName, inManagedObjectContext: context!)!
let entityDescription: NSEntityDescription = NSEntityDescription.entityForName(groupDescriptionName, inManagedObjectContext: Graph.context!)!
let request: NSFetchRequest = NSFetchRequest()
request.entity = entityDescription
request.fetchBatchSize = batchSize
Expand All @@ -229,7 +229,7 @@ public extension Graph {
request.predicate = NSCompoundPredicate(orPredicateWithSubpredicates: groupsPredicate)
request.sortDescriptors = [NSSortDescriptor(key: "node.createdDate", ascending: true)]

return try? context!.executeFetchRequest(request)
return try? Graph.context!.executeFetchRequest(request)
}

internal func search(propertyDescriptionName: String, properties: Array<(key: String, value: AnyObject?)>) -> Array<AnyObject>? {
Expand All @@ -247,7 +247,7 @@ public extension Graph {
}
}

let entityDescription: NSEntityDescription = NSEntityDescription.entityForName(propertyDescriptionName, inManagedObjectContext: context!)!
let entityDescription: NSEntityDescription = NSEntityDescription.entityForName(propertyDescriptionName, inManagedObjectContext: Graph.context!)!
let request: NSFetchRequest = NSFetchRequest()
request.entity = entityDescription
request.fetchBatchSize = batchSize
Expand All @@ -258,6 +258,6 @@ public extension Graph {
request.predicate = NSCompoundPredicate(orPredicateWithSubpredicates: propertiesPredicate)
request.sortDescriptors = [NSSortDescriptor(key: "node.createdDate", ascending: true)]

return try? context!.executeFetchRequest(request)
return try? Graph.context!.executeFetchRequest(request)
}
}
6 changes: 3 additions & 3 deletions Sources/GraphWatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public extension Graph {
//
internal func prepareForObservation() {
NSNotificationCenter.defaultCenter().removeObserver(self)
NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextDidSaveNotification, object: context)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "managedObjectContextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: context)
NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextDidSaveNotification, object: Graph.context)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "managedObjectContextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: Graph.context)
}

//
Expand Down Expand Up @@ -230,7 +230,7 @@ public extension Graph {
save operation.
*/
private func notifyWatchers(notification: NSNotification) {
let moc: NSManagedObjectContext = context!
let moc: NSManagedObjectContext = Graph.context!
moc.mergeChangesFromContextDidSaveNotification(notification)

let userInfo: [NSObject : AnyObject]? = notification.userInfo
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.5</string>
<string>1.0.6</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
5 changes: 2 additions & 3 deletions Sources/ManagedAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ internal class ManagedAction : ManagedNode {
:description: Initializes the Model Object with e a given type.
*/
internal convenience init(type: String) {
let g: Graph = Graph()
self.init(entity: NSEntityDescription.entityForName(GraphUtility.actionDescriptionName, inManagedObjectContext: g.context!)!, insertIntoManagedObjectContext: g.context)
self.init(entity: NSEntityDescription.entityForName(GraphUtility.actionDescriptionName, inManagedObjectContext: Graph.context!)!, insertIntoManagedObjectContext: Graph.context)
nodeClass = NodeClass.Action.rawValue
self.type = type
createdDate = NSDate()
propertySet = NSSet()
groupSet = NSSet()
subjectSet = NSSet()
objectSet = NSSet()
context = g.context
context = Graph.context
}

/**
Expand Down
5 changes: 2 additions & 3 deletions Sources/ManagedActionGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ internal class ManagedActionGroup : ManagedNodeGroup {
:description: Initializer for the Model Object.
*/
internal convenience init(name: String) {
let g: Graph = Graph()
self.init(entity: NSEntityDescription.entityForName(GraphUtility.actionGroupDescriptionName, inManagedObjectContext: g.context!)!, insertIntoManagedObjectContext: g.context)
self.init(entity: NSEntityDescription.entityForName(GraphUtility.actionGroupDescriptionName, inManagedObjectContext: Graph.context!)!, insertIntoManagedObjectContext: Graph.context)
self.name = name
context = g.context
context = Graph.context
}
}
5 changes: 2 additions & 3 deletions Sources/ManagedActionProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ internal class ManagedActionProperty : ManagedNodeProperty {
:description: Initializer for the Model Object.
*/
internal convenience init(name: String, object: AnyObject) {
let g: Graph = Graph()
self.init(entity: NSEntityDescription.entityForName(GraphUtility.actionPropertyDescriptionName, inManagedObjectContext: g.context!)!, insertIntoManagedObjectContext: g.context)
self.init(entity: NSEntityDescription.entityForName(GraphUtility.actionPropertyDescriptionName, inManagedObjectContext: Graph.context!)!, insertIntoManagedObjectContext: Graph.context)
self.name = name
self.object = object
context = g.context
context = Graph.context
}
}
5 changes: 2 additions & 3 deletions Sources/ManagedEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ internal class ManagedEntity : ManagedNode {
:description: Initializes the Model Object with e a given type.
*/
internal convenience init(type: String) {
let g: Graph = Graph()
self.init(entity: NSEntityDescription.entityForName(GraphUtility.entityDescriptionName, inManagedObjectContext: g.context!)!, insertIntoManagedObjectContext: g.context)
self.init(entity: NSEntityDescription.entityForName(GraphUtility.entityDescriptionName, inManagedObjectContext: Graph.context!)!, insertIntoManagedObjectContext: Graph.context)
nodeClass = NodeClass.Entity.rawValue
self.type = type
createdDate = NSDate()
Expand All @@ -52,7 +51,7 @@ internal class ManagedEntity : ManagedNode {
actionObjectSet = NSSet()
relationshipSubjectSet = NSSet()
relationshipObjectSet = NSSet()
context = g.context
context = Graph.context
}

/**
Expand Down
5 changes: 2 additions & 3 deletions Sources/ManagedEntityGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ internal class ManagedEntityGroup : ManagedNodeGroup {
:description: Initializer for the Model Object.
*/
internal convenience init(name: String) {
let g: Graph = Graph()
self.init(entity: NSEntityDescription.entityForName(GraphUtility.entityGroupDescriptionName, inManagedObjectContext: g.context!)!, insertIntoManagedObjectContext: g.context)
self.init(entity: NSEntityDescription.entityForName(GraphUtility.entityGroupDescriptionName, inManagedObjectContext: Graph.context!)!, insertIntoManagedObjectContext: Graph.context)
self.name = name
context = g.context
context = Graph.context
}
}
5 changes: 2 additions & 3 deletions Sources/ManagedEntityProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ internal class ManagedEntityProperty : ManagedNodeProperty {
:name: init
*/
internal convenience init(name: String, object: AnyObject) {
let g: Graph = Graph()
self.init(entity: NSEntityDescription.entityForName(GraphUtility.entityPropertyDescriptionName, inManagedObjectContext: g.context!)!, insertIntoManagedObjectContext: g.context)
self.init(entity: NSEntityDescription.entityForName(GraphUtility.entityPropertyDescriptionName, inManagedObjectContext: Graph.context!)!, insertIntoManagedObjectContext: Graph.context)
self.name = name
self.object = object
context = g.context
context = Graph.context
}
}
Loading

0 comments on commit 04adf36

Please sign in to comment.