Skip to content

Commit

Permalink
wrap methods that take in NSErrorPointer in universal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-v committed Dec 11, 2014
1 parent f1ed021 commit d881a79
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 54 deletions.
12 changes: 6 additions & 6 deletions PodSpecImporter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
EA9B36851A30F8C400CC5448 /* Pod+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9B367F1A30F75000CC5448 /* Pod+Extension.swift */; };
EA9B36881A31A41000CC5448 /* CoreDataPersistenceStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9B36871A31A41000CC5448 /* CoreDataPersistenceStack.swift */; };
EA9B36891A31A41000CC5448 /* CoreDataPersistenceStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9B36871A31A41000CC5448 /* CoreDataPersistenceStack.swift */; };
EAC693791A3616ED00A4FA99 /* ErrorReferenceHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC693781A3616ED00A4FA99 /* ErrorReferenceHandler.swift */; };
EAC6937A1A36178900A4FA99 /* ErrorReferenceHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC693781A3616ED00A4FA99 /* ErrorReferenceHandler.swift */; };
EAC693791A3616ED00A4FA99 /* ErrorReferenceHandling.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC693781A3616ED00A4FA99 /* ErrorReferenceHandling.swift */; };
EAC6937A1A36178900A4FA99 /* ErrorReferenceHandling.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC693781A3616ED00A4FA99 /* ErrorReferenceHandling.swift */; };
EAC6937D1A370C6F00A4FA99 /* PodListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC6937C1A370C6F00A4FA99 /* PodListViewController.swift */; };
EAC6937F1A370D2500A4FA99 /* FetchedResultsControllerDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC6937E1A370D2500A4FA99 /* FetchedResultsControllerDataSource.swift */; };
EAC693811A372E3900A4FA99 /* TableViewFetchedResultsControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC693801A372E3900A4FA99 /* TableViewFetchedResultsControllerDelegate.swift */; };
Expand Down Expand Up @@ -70,7 +70,7 @@
EA9B367F1A30F75000CC5448 /* Pod+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Pod+Extension.swift"; sourceTree = "<group>"; };
EA9B36811A30F78000CC5448 /* PodTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PodTests.swift; sourceTree = "<group>"; };
EA9B36871A31A41000CC5448 /* CoreDataPersistenceStack.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreDataPersistenceStack.swift; sourceTree = "<group>"; };
EAC693781A3616ED00A4FA99 /* ErrorReferenceHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorReferenceHandler.swift; sourceTree = "<group>"; };
EAC693781A3616ED00A4FA99 /* ErrorReferenceHandling.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorReferenceHandling.swift; sourceTree = "<group>"; };
EAC6937C1A370C6F00A4FA99 /* PodListViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PodListViewController.swift; path = GUI/PodListViewController.swift; sourceTree = "<group>"; };
EAC6937E1A370D2500A4FA99 /* FetchedResultsControllerDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchedResultsControllerDataSource.swift; sourceTree = "<group>"; };
EAC693801A372E3900A4FA99 /* TableViewFetchedResultsControllerDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewFetchedResultsControllerDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,7 +125,7 @@
EAC6937B1A370C4B00A4FA99 /* GUI */,
EA9B36761A30F2AD00CC5448 /* WebJSONService */,
EA9B36641A30EDBB00CC5448 /* AppDelegate.swift */,
EAC693781A3616ED00A4FA99 /* ErrorReferenceHandler.swift */,
EAC693781A3616ED00A4FA99 /* ErrorReferenceHandling.swift */,
EA9B36401A30ED7B00CC5448 /* Supporting Files */,
);
path = PodSpecImporter;
Expand Down Expand Up @@ -318,7 +318,7 @@
EA9B36691A30EDBB00CC5448 /* WebJSONService.swift in Sources */,
EA9B36781A30F2C000CC5448 /* Importer.swift in Sources */,
EAC693841A3732B700A4FA99 /* UIStoryboardInjector.swift in Sources */,
EAC693791A3616ED00A4FA99 /* ErrorReferenceHandler.swift in Sources */,
EAC693791A3616ED00A4FA99 /* ErrorReferenceHandling.swift in Sources */,
EAC6937F1A370D2500A4FA99 /* FetchedResultsControllerDataSource.swift in Sources */,
EA9B36801A30F75000CC5448 /* Pod+Extension.swift in Sources */,
EA9B36681A30EDBB00CC5448 /* UpdatePodsUseCase.swift in Sources */,
Expand All @@ -333,7 +333,7 @@
EA9B36891A31A41000CC5448 /* CoreDataPersistenceStack.swift in Sources */,
EA9B36851A30F8C400CC5448 /* Pod+Extension.swift in Sources */,
EA9B36711A30EDE700CC5448 /* WebJSONService.swift in Sources */,
EAC6937A1A36178900A4FA99 /* ErrorReferenceHandler.swift in Sources */,
EAC6937A1A36178900A4FA99 /* ErrorReferenceHandling.swift in Sources */,
EA9B36751A30EDE700CC5448 /* Result.swift in Sources */,
EAD5B83B1A35DE2800D81243 /* Pod.swift in Sources */,
EA9B366F1A30EDD900CC5448 /* PodsJSONServiceTests.swift in Sources */,
Expand Down
21 changes: 9 additions & 12 deletions PodSpecImporter/CoreDataPersistenceStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,30 @@ class PersistenceStack {
context = setupContextWithConcurrencyType(.MainQueueConcurrencyType, model: model, storeURL: storeURL)
backgroundContext = setupContextWithConcurrencyType(.PrivateQueueConcurrencyType, model: model, storeURL: storeURL)

listenForNotifications()
listenForNotifications()
}

private func setupContextWithConcurrencyType(type: NSManagedObjectContextConcurrencyType, model: NSManagedObjectModel, storeURL: NSURL) -> NSManagedObjectContext {
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
var possibleError: NSError?
coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &possibleError)
if let error = possibleError {
abort()
}
tryWithError { coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: $0) }
.onError { _ in abort() }
var context = NSManagedObjectContext(concurrencyType: type)
context.persistentStoreCoordinator = coordinator
return context
}

private func listenForNotifications() {
NSNotificationCenter.defaultCenter().addObserverForName(NSManagedObjectContextDidSaveNotification, object: nil, queue: nil) {
NSNotificationCenter.defaultCenter().addObserverForName(NSManagedObjectContextDidSaveNotification, object: backgroundContext, queue: nil) {
[weak self] notification in
if let context = self?.context {
if context != notification.object as? NSManagedObjectContext {
context.performBlock { context.mergeChangesFromContextDidSaveNotification(notification) }
}
context.performBlock { context.mergeChangesFromContextDidSaveNotification(notification) }
}
}
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationWillTerminateNotification, object: nil, queue: nil) {
[weak self] _ in self?.context.save(nil); return
[weak self] _ in
tryWithError { self?.context.save($0) }
.onError { error in println("termination save error: \(error)") }
return
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ErrorReference.swift
// ErrorReferenceHandling
// PodSpecImporter
//
// Created by Witold Skibniewski on 08/12/14.
Expand Down
26 changes: 13 additions & 13 deletions PodSpecImporter/Importer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ class Importer {

context.performBlock { [weak self] in
if let context = self?.context {
var possibleError: NSError?
let result = context.executeFetchRequest(existingSpecsRequest, error: &possibleError) as [Pod]
if let error = possibleError {
return // + log
}
var existingPods = [String: Pod]()
for pod in result { existingPods[pod.identifier] = pod }
for data in JSONSpecs {
let identifier: String! = self?.identifierFromData(data)
var existing = existingPods[identifier]
var pod = existing ?? NSEntityDescription.insertNewObjectForEntityForName("Pod", inManagedObjectContext: context) as Pod
pod.loadFromJSONObject(data)
}
tryWithError { errorPointer in context.executeFetchRequest(existingSpecsRequest, error: errorPointer) as [Pod] }
.onSuccess { result in
var existingPods = [String: Pod]()
for pod in result { existingPods[pod.identifier] = pod }
for data in JSONSpecs {
let identifier: String! = self?.identifierFromData(data)
var existing = existingPods[identifier]
var pod = existing ?? NSEntityDescription.insertNewObjectForEntityForName("Pod", inManagedObjectContext: context) as Pod
pod.loadFromJSONObject(data)
}
}
.onError { println("error on pods fetch: \($0)") }
}

}
}

Expand Down
20 changes: 3 additions & 17 deletions PodSpecImporter/WebJSONService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,9 @@ class WebJSONService {
}
}

var possibleError: NSError?
var deserialized = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &possibleError) as? NSDictionary
if let error = possibleError {
dispatchError()
return
}
if let jsonObject = deserialized {
if jsonObject["stat"] as? String == "fail" {
dispatchError()
return
}

dispatch_async(dispatch_get_main_queue()) { completionHandler(.OK(jsonObject)) }
} else {
dispatchError()
}

tryWithError{ NSJSONSerialization.JSONObjectWithData(data, options: nil, error: $0) as? NSDictionary }
.onError { _ in dispatchError() }
.onSuccess { jsonObject in dispatch_async(dispatch_get_main_queue()) { completionHandler(.OK(jsonObject!)) } }
})
task.resume()
}
Expand Down
9 changes: 4 additions & 5 deletions PodSpecImporterTests/PodTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ class PodTests: XCTestCase {
private func makePod() -> Pod? {
let model = makeCoreDataModel()
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
var possibleError: NSError?
coordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil, error: &possibleError)
if let error = possibleError {
println(error)
abort()
tryWithError { coordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil, error: $0) }
.onError {
println($0)
abort()
}

context = NSManagedObjectContext()
Expand Down

0 comments on commit d881a79

Please sign in to comment.