diff --git a/CollectionSwift/AppDelegate.swift b/CollectionSwift/AppDelegate.swift index 4d94964..b37cf37 100644 --- a/CollectionSwift/AppDelegate.swift +++ b/CollectionSwift/AppDelegate.swift @@ -14,6 +14,7 @@ class AppDelegate: UIResponder { } extension AppDelegate: UIApplicationDelegate { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) guard let window = self.window else { fatalError("Window not found") } diff --git a/CollectionSwift/CollectionController.swift b/CollectionSwift/CollectionController.swift index b9abf0b..a38dad4 100644 --- a/CollectionSwift/CollectionController.swift +++ b/CollectionSwift/CollectionController.swift @@ -12,7 +12,7 @@ class CollectionController: UICollectionViewController { let request: NSFetchRequest = NSFetchRequest(entityName: "User") request.sortDescriptors = [ NSSortDescriptor(key: "name", ascending: true), - NSSortDescriptor(key: "firstLetterOfName", ascending: true) + NSSortDescriptor(key: "firstLetterOfName", ascending: true), ] let dataSource = DATASource(collectionView: collectionView, cellIdentifier: CollectionCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in @@ -21,7 +21,7 @@ class CollectionController: UICollectionViewController { }) return dataSource - }() + }() init(layout: UICollectionViewLayout, dataStack: DATAStack) { self.dataStack = dataStack @@ -71,22 +71,23 @@ class CollectionController: UICollectionViewController { func randomString() -> String { let letters = "ABCDEFGHIJKL" var string = "" - for _ in 0...5 { + for _ in 0 ... 5 { let token = UInt32(letters.characters.count) let letterIndex = Int(arc4random_uniform(token)) let firstChar = Array(letters.characters)[letterIndex] string.append(firstChar) } - + return string } } extension CollectionController { + override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { guard let object = self.dataSource.objectAtIndexPath(indexPath) else { return } - if let name = object.value(forKey: "name") as? String , name.characters.first == "A" { + if let name = object.value(forKey: "name") as? String, name.characters.first == "A" { self.dataStack.performInNewBackgroundContext({ backgroundContext in let backgroundObject = backgroundContext.object(with: object.objectID) backgroundObject.setValue(name + "+", forKey: "name") diff --git a/DATASourceDelegateTableSwift/AppDelegate.swift b/DATASourceDelegateTableSwift/AppDelegate.swift index 9933638..c0cb585 100644 --- a/DATASourceDelegateTableSwift/AppDelegate.swift +++ b/DATASourceDelegateTableSwift/AppDelegate.swift @@ -14,6 +14,7 @@ class AppDelegate: UIResponder { } extension AppDelegate: UIApplicationDelegate { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) guard let window = self.window else { fatalError("Window not found") } @@ -21,7 +22,7 @@ extension AppDelegate: UIApplicationDelegate { let viewController = ViewController(dataStack: self.dataStack) window.rootViewController = UINavigationController(rootViewController: viewController) window.makeKeyAndVisible() - + return true } } diff --git a/DATASourceDelegateTableSwift/ViewController.swift b/DATASourceDelegateTableSwift/ViewController.swift index f5b6667..a765ee6 100644 --- a/DATASourceDelegateTableSwift/ViewController.swift +++ b/DATASourceDelegateTableSwift/ViewController.swift @@ -51,19 +51,20 @@ class ViewController: UITableViewController { } extension ViewController: DATASourceDelegate { + func dataSource(_ dataSource: DATASource, tableView: UITableView, canEditRowAtIndexPath indexPath: IndexPath) -> Bool { return true } // This doesn't seem to be needed when implementing tableView(_:editActionsForRowAtIndexPath). func dataSource(_ dataSource: DATASource, tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: IndexPath) { - } } // MARK: - UITableViewDelegate extension ViewController { + override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .default, title: "Delete") { action, indexPath in let item = self.dataSource.objectAtIndexPath(indexPath)! diff --git a/InfiniteCollectionSwift/AppDelegate.swift b/InfiniteCollectionSwift/AppDelegate.swift index b8b38f6..b37cf37 100644 --- a/InfiniteCollectionSwift/AppDelegate.swift +++ b/InfiniteCollectionSwift/AppDelegate.swift @@ -14,6 +14,7 @@ class AppDelegate: UIResponder { } extension AppDelegate: UIApplicationDelegate { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) guard let window = self.window else { fatalError("Window not found") } @@ -28,7 +29,7 @@ extension AppDelegate: UIApplicationDelegate { let viewController = CollectionController(layout: layout, dataStack: self.dataStack) window.rootViewController = UINavigationController(rootViewController: viewController) window.makeKeyAndVisible() - + return true } } diff --git a/InfiniteCollectionSwift/CollectionController.swift b/InfiniteCollectionSwift/CollectionController.swift index c78013d..68d2fd7 100644 --- a/InfiniteCollectionSwift/CollectionController.swift +++ b/InfiniteCollectionSwift/CollectionController.swift @@ -12,7 +12,7 @@ class CollectionController: UICollectionViewController { let request: NSFetchRequest = NSFetchRequest(entityName: "User") request.sortDescriptors = [ NSSortDescriptor(key: "name", ascending: true), - NSSortDescriptor(key: "firstLetterOfName", ascending: true) + NSSortDescriptor(key: "firstLetterOfName", ascending: true), ] let dataSource = DATASource(collectionView: collectionView, cellIdentifier: CollectionCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in @@ -57,6 +57,7 @@ class CollectionController: UICollectionViewController { } var loading = false + override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { guard let collectionView = self.collectionView else { return } guard self.loading == false else { return } @@ -80,7 +81,7 @@ class CollectionController: UICollectionViewController { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { self.dataStack.performInNewBackgroundContext { backgroundContext in let entity = NSEntityDescription.entity(forEntityName: "User", in: backgroundContext)! - for i in initialIndex.. Bool { self.window = UIWindow(frame: UIScreen.main.bounds) guard let window = self.window else { fatalError("Window not found") } diff --git a/InfiniteTableSwift/ViewController.swift b/InfiniteTableSwift/ViewController.swift index c4dd89b..6c46137 100644 --- a/InfiniteTableSwift/ViewController.swift +++ b/InfiniteTableSwift/ViewController.swift @@ -10,7 +10,7 @@ class ViewController: UITableViewController { let request: NSFetchRequest = NSFetchRequest(entityName: "User") request.sortDescriptors = [ NSSortDescriptor(key: "name", ascending: true), - NSSortDescriptor(key: "firstLetterOfName", ascending: true) + NSSortDescriptor(key: "firstLetterOfName", ascending: true), ] let dataSource = DATASource(tableView: self.tableView, cellIdentifier: ViewController.Identifier, fetchRequest: request, mainContext: self.dataStack!.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in @@ -49,6 +49,7 @@ class ViewController: UITableViewController { } var loading = false + override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { guard let tableView = self.tableView else { return } guard self.loading == false else { return } @@ -72,7 +73,7 @@ class ViewController: UITableViewController { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { self.dataStack!.performInNewBackgroundContext { backgroundContext in let entity = NSEntityDescription.entity(forEntityName: "User", in: backgroundContext)! - for i in initialIndex.. [String] { return [String]() } diff --git a/Library/InfiniteLoadingIndicator.swift b/Library/InfiniteLoadingIndicator.swift index cd30f47..71d665e 100644 --- a/Library/InfiniteLoadingIndicator.swift +++ b/Library/InfiniteLoadingIndicator.swift @@ -47,7 +47,7 @@ class InfiniteLoadingIndicator: UIView { UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [.allowUserInteraction], animations: { self.frame = originalFrame - }, completion: nil) + }, completion: nil) } } @@ -58,11 +58,11 @@ class InfiniteLoadingIndicator: UIView { newFrame.origin.y = self.parentController.view.frame.height + 10 UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [.allowUserInteraction], animations: { () -> Void in self.frame = newFrame - }, completion: { finished in - self.frame = originalFrame - if self.superview != nil { - self.removeFromSuperview() - } + }, completion: { finished in + self.frame = originalFrame + if self.superview != nil { + self.removeFromSuperview() + } }) } } diff --git a/Source/DATASource+NSFetchedResultsControllerDelegate.swift b/Source/DATASource+NSFetchedResultsControllerDelegate.swift index 9783875..e0f9a11 100644 --- a/Source/DATASource+NSFetchedResultsControllerDelegate.swift +++ b/Source/DATASource+NSFetchedResultsControllerDelegate.swift @@ -2,12 +2,13 @@ import UIKit import CoreData extension DATASource: NSFetchedResultsControllerDelegate { + public func controllerWillChangeContent(_ controller: NSFetchedResultsController) { if let tableView = self.tableView { tableView.beginUpdates() } else if let _ = self.collectionView { - self.sectionChanges = [NSFetchedResultsChangeType : IndexSet]() - self.objectChanges = [NSFetchedResultsChangeType : Set]() + self.sectionChanges = [NSFetchedResultsChangeType: IndexSet]() + self.objectChanges = [NSFetchedResultsChangeType: Set]() } } @@ -190,7 +191,7 @@ extension DATASource: NSFetchedResultsControllerDelegate { collectionView.moveItem(at: fromIndexPath as IndexPath, to: toIndexPath as IndexPath) } - }, completion: nil) + }, completion: nil) } } self.delegate?.dataSourceDidChangeContent?(self) diff --git a/Source/DATASource+UICollectionViewDataSource.swift b/Source/DATASource+UICollectionViewDataSource.swift index 8b10207..94c2be2 100644 --- a/Source/DATASource+UICollectionViewDataSource.swift +++ b/Source/DATASource+UICollectionViewDataSource.swift @@ -2,6 +2,7 @@ import UIKit import CoreData extension DATASource: UICollectionViewDataSource { + public func numberOfSections(in collectionView: UICollectionView) -> Int { return self.fetchedResultsController.sections?.count ?? 0 } @@ -32,7 +33,7 @@ extension DATASource: UICollectionViewDataSource { public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { if let keyPath = self.fetchedResultsController.sectionNameKeyPath { - if self.cachedSectionNames.isEmpty || indexPath.section >= self.cachedSectionNames.count{ + if self.cachedSectionNames.isEmpty || indexPath.section >= self.cachedSectionNames.count { self.cacheSectionNames(using: keyPath) } diff --git a/Source/DATASource+UITableViewDataSource.swift b/Source/DATASource+UITableViewDataSource.swift index 5ae07fe..c8915dc 100644 --- a/Source/DATASource+UITableViewDataSource.swift +++ b/Source/DATASource+UITableViewDataSource.swift @@ -2,6 +2,7 @@ import UIKit import CoreData extension DATASource: UITableViewDataSource { + public func numberOfSections(in tableView: UITableView) -> Int { return self.fetchedResultsController.sections?.count ?? 0 } @@ -17,14 +18,14 @@ extension DATASource: UITableViewDataSource { } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - + var cellIdentifier = self.cellIdentifier - + if let value = self.delegate?.cellIdentifier?(forIndexPath: indexPath) { cellIdentifier = value } - - let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) + + let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) self.configure(cell, indexPath: indexPath) @@ -34,40 +35,41 @@ extension DATASource: UITableViewDataSource { // Sections and Headers #if os(iOS) - public func sectionIndexTitles(for tableView: UITableView) -> [String]? { - if let titles = self.delegate?.sectionIndexTitlesForDataSource?(self, tableView: tableView) { - return titles - } else if let keyPath = self.fetchedResultsController.sectionNameKeyPath { - let request = NSFetchRequest() - request.entity = self.fetchedResultsController.fetchRequest.entity - request.resultType = .dictionaryResultType - request.returnsDistinctResults = true - request.propertiesToFetch = [keyPath] - request.sortDescriptors = [NSSortDescriptor(key: keyPath, ascending: true)] - var names = [String]() - var objects: [NSDictionary]? - - do { - objects = try self.fetchedResultsController.managedObjectContext.fetch(request) as? [NSDictionary] - } catch { - print("Error") - } - if let objects = objects { - for object in objects { - names.append(contentsOf: object.allValues as! [String]) + public func sectionIndexTitles(for tableView: UITableView) -> [String]? { + if let titles = self.delegate?.sectionIndexTitlesForDataSource?(self, tableView: tableView) { + return titles + } else if let keyPath = self.fetchedResultsController.sectionNameKeyPath { + let request = NSFetchRequest() + request.entity = self.fetchedResultsController.fetchRequest.entity + request.resultType = .dictionaryResultType + request.returnsDistinctResults = true + request.propertiesToFetch = [keyPath] + request.sortDescriptors = [NSSortDescriptor(key: keyPath, ascending: true)] + var names = [String]() + var objects: [NSDictionary]? + + do { + objects = try self.fetchedResultsController.managedObjectContext.fetch(request) as? [NSDictionary] + } catch { + print("Error") + } + + if let objects = objects { + for object in objects { + names.append(contentsOf: object.allValues as! [String]) + } } + + return names } - return names + return nil } - return nil - } - - public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { - return self.delegate?.dataSource?(self, tableView: tableView, sectionForSectionIndexTitle: title, atIndex: index) ?? index - } + public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { + return self.delegate?.dataSource?(self, tableView: tableView, sectionForSectionIndexTitle: title, atIndex: index) ?? index + } #endif public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { diff --git a/Source/DATASource.swift b/Source/DATASource.swift index 3e6492e..aa4d84d 100644 --- a/Source/DATASource.swift +++ b/Source/DATASource.swift @@ -2,6 +2,7 @@ import UIKit import CoreData public class DATASource: NSObject { + /** Initializes and returns a data source object for a table view. - parameter tableView: A table view used to construct the data source. @@ -33,7 +34,7 @@ public class DATASource: NSObject { self.collectionView = collectionView self.collectionView?.dataSource = self - self.collectionView?.register(DATASourceCollectionViewHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: DATASourceCollectionViewHeader.Identifier); + self.collectionView?.register(DATASourceCollectionViewHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: DATASourceCollectionViewHeader.Identifier) } /** @@ -100,13 +101,13 @@ public class DATASource: NSObject { var fetchedResultsController: NSFetchedResultsController - lazy var objectChanges: [NSFetchedResultsChangeType : Set] = { - return [NSFetchedResultsChangeType : Set]() + lazy var objectChanges: [NSFetchedResultsChangeType: Set] = { + return [NSFetchedResultsChangeType: Set]() }() - lazy var sectionChanges: [NSFetchedResultsChangeType : IndexSet] = { - return [NSFetchedResultsChangeType : IndexSet]() + lazy var sectionChanges: [NSFetchedResultsChangeType: IndexSet] = { + return [NSFetchedResultsChangeType: IndexSet]() }() lazy var cachedSectionNames: [Any] = { @@ -127,7 +128,7 @@ public class DATASource: NSObject { self.fetch() self.tableView?.reloadData() - if let visibleIndexPaths = self.collectionView?.indexPathsForVisibleItems , visibleIndexPaths.count > 0 { + if let visibleIndexPaths = self.collectionView?.indexPathsForVisibleItems, visibleIndexPaths.count > 0 { self.collectionView?.reloadItems(at: visibleIndexPaths) } } @@ -181,7 +182,7 @@ public class DATASource: NSObject { All the objects fetched by DATASource. This is an array of `NSManagedObject`. */ public func all() -> [T] { - return self.fetchedResultsController.fetchedObjects as? [T] ?? [T]() + return self.fetchedResultsController.fetchedObjects as? [T] ?? [T]() } /** @@ -309,5 +310,5 @@ public class DATASource: NSObject { } } } - } + } } diff --git a/Source/DATASourceCollectionHeaderView.swift b/Source/DATASourceCollectionHeaderView.swift index 7586a48..69184f2 100644 --- a/Source/DATASourceCollectionHeaderView.swift +++ b/Source/DATASourceCollectionHeaderView.swift @@ -36,7 +36,7 @@ public class DATASourceCollectionViewHeader: UICollectionReusableView { self.addSubview(self.separatorView) } - required public init?(coder aDecoder: NSCoder) { + public required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } diff --git a/Source/DATASourceDelegate.swift b/Source/DATASourceDelegate.swift index 8987747..e198450 100644 --- a/Source/DATASourceDelegate.swift +++ b/Source/DATASourceDelegate.swift @@ -9,7 +9,7 @@ import CoreData * * ************************** */ - @objc optional func cellIdentifier(forIndexPath:IndexPath) -> String + @objc optional func cellIdentifier(forIndexPath: IndexPath) -> String @objc optional func dataSource(_ dataSource: DATASource, configureTableViewCell cell: UITableViewCell, withItem item: NSManagedObject, atIndexPath indexPath: IndexPath) @objc optional func dataSource(_ dataSource: DATASource, configureCollectionViewCell cell: UICollectionViewCell, withItem item: NSManagedObject, atIndexPath indexPath: IndexPath) @@ -27,12 +27,12 @@ import CoreData @objc optional func dataSourceDidChangeContent(_ dataSource: DATASource) /*! - * ************************** - * - * UITableView - * - * ************************** - */ + * ************************** + * + * UITableView + * + * ************************** + */ // Sections and Headers @@ -52,12 +52,12 @@ import CoreData @objc optional func dataSource(_ dataSource: DATASource, tableView: UITableView, moveRowAtIndexPath sourceIndexPath: IndexPath, toIndexPath destinationIndexPath: IndexPath) /*! - * ************************** - * - * UICollectionView - * - * ************************** - */ + * ************************** + * + * UICollectionView + * + * ************************** + */ @objc optional func dataSource(_ dataSource: DATASource, collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: IndexPath, withTitle title: Any?) -> UICollectionReusableView? } diff --git a/TableSwift/AppDelegate.swift b/TableSwift/AppDelegate.swift index 9933638..c0cb585 100755 --- a/TableSwift/AppDelegate.swift +++ b/TableSwift/AppDelegate.swift @@ -14,6 +14,7 @@ class AppDelegate: UIResponder { } extension AppDelegate: UIApplicationDelegate { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) guard let window = self.window else { fatalError("Window not found") } @@ -21,7 +22,7 @@ extension AppDelegate: UIApplicationDelegate { let viewController = ViewController(dataStack: self.dataStack) window.rootViewController = UINavigationController(rootViewController: viewController) window.makeKeyAndVisible() - + return true } } diff --git a/TableSwift/ViewController.swift b/TableSwift/ViewController.swift index a09593d..002747b 100644 --- a/TableSwift/ViewController.swift +++ b/TableSwift/ViewController.swift @@ -10,7 +10,7 @@ class ViewController: UITableViewController { request.sortDescriptors = [ NSSortDescriptor(key: "firstLetterOfName", ascending: true), NSSortDescriptor(key: "count", ascending: true), - NSSortDescriptor(key: "name", ascending: true) + NSSortDescriptor(key: "name", ascending: true), ] let dataSource = DATASource(tableView: self.tableView, cellIdentifier: CustomCell.Identifier, fetchRequest: request, mainContext: self.dataStack!.mainContext, sectionName: "firstLetterOfName") @@ -62,7 +62,7 @@ class ViewController: UITableViewController { func randomString() -> String { let letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZÅØÆ" var string = "" - for _ in 0...10 { + for _ in 0 ... 10 { let token = UInt32(letters.characters.count) let letterIndex = Int(arc4random_uniform(token)) let firstChar = Array(letters.characters)[letterIndex] @@ -86,6 +86,7 @@ class ViewController: UITableViewController { } extension ViewController: DATASourceDelegate { + func dataSource(_ dataSource: DATASource, configureTableViewCell cell: UITableViewCell, withItem item: NSManagedObject, atIndexPath indexPath: IndexPath) { if let cell = cell as? CustomCell { let name = item.value(forKey: "name") as? String ?? "" diff --git a/Tests/Tests.swift b/Tests/Tests.swift index 6999ac8..8767563 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -46,33 +46,33 @@ class Tests: XCTestCase { } /* - func testCollectionViewDataSouce() { - var success = false - let bundle = NSBundle(forClass: Tests.self) - let dataStack = DATAStack(modelName: Tests.ModelName, bundle: bundle, storeType: .InMemory) - let layout = UICollectionViewLayout() - let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout) - collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: Tests.CellIdentifier) - let request = NSFetchRequest(entityName: Tests.EntityName) - request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)] + func testCollectionViewDataSouce() { + var success = false + let bundle = NSBundle(forClass: Tests.self) + let dataStack = DATAStack(modelName: Tests.ModelName, bundle: bundle, storeType: .InMemory) + let layout = UICollectionViewLayout() + let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout) + collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: Tests.CellIdentifier) + let request = NSFetchRequest(entityName: Tests.EntityName) + request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)] - let dataSource = DATASource(collectionView: collectionView, cellIdentifier: Tests.CellIdentifier, fetchRequest: request, mainContext: dataStack.mainContext) { cell, item, indexPath in - success = true - } - collectionView.dataSource = dataSource - collectionView.reloadData() + let dataSource = DATASource(collectionView: collectionView, cellIdentifier: Tests.CellIdentifier, fetchRequest: request, mainContext: dataStack.mainContext) { cell, item, indexPath in + success = true + } + collectionView.dataSource = dataSource + collectionView.reloadData() - dataStack.performInNewBackgroundContext { backgroundContext in - self.userWithName("Elvis", context: backgroundContext) - try! backgroundContext.save() - } + dataStack.performInNewBackgroundContext { backgroundContext in + self.userWithName("Elvis", context: backgroundContext) + try! backgroundContext.save() + } - XCTAssertTrue(success) + XCTAssertTrue(success) - // Fails - // CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. - // Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), - // plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). with userInfo (null) - } - */ + // Fails + // CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. + // Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), + // plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). with userInfo (null) + } + */ }