Today when i was making simple test
func testCountSales() {
measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: {
let employee = self.getEmployees()
let employeeDetails = EmployeeDetailViewController()
self.startMeasuring()
_ = employeeDetails.salesCountForEmployees(employee)
self.stopMeasuring()
})
}
func getEmployees() -> Employee {
let coreDataStack = CoreDataStack(modelName: "EmployeeDirectory")
let request: NSFetchRequest<Employee> = NSFetchRequest(entityName: "Employee")
request.sortDescriptors = [NSSortDescriptor(key: "guid", ascending: true)]
request.fetchBatchSize = 1
let results: [AnyObject]?
do {
results = try coreDataStack.mainContext.fetch(request)
} catch _ {
results = nil
}
return results![0] as! Employee
}
I wondered do fetchBachSize really working? I tried to see debug section the was full array (50 elements as it supposed to be).All of them were faults. ok. Then i tried to add Observer for FetchedResultsController's fetchedObject's count property
var window: UIWindow?
lazy var coreDataStack = CoreDataStack(modelName: "EmployeeDirectory")
let amountToImport = 50
let addSalesRecords = true
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
importJSONSeedDataIfNeeded()
guard let tabController = window?.rootViewController as? UITabBarController,
let employeeListNavigationController = tabController.viewControllers?[0] as? UINavigationController,
let employeeListViewController = employeeListNavigationController.topViewController as? EmployeeListViewController else {
fatalError("Application storyboard mis-configuration. Application is mis-configured")
}
employeeListViewController.coreDataStack = coreDataStack
employeeListViewController.addObserver(self, forKeyPath: "fetchedResultsController", options: [.new], context: nil)
guard let departmentListNavigationController = tabController.viewControllers?[1] as? UINavigationController,
let departmentListViewController = departmentListNavigationController.topViewController as? DepartmentListViewController else {
fatalError("Application storyboard mis-configuration. Application is mis-configured")
}
departmentListViewController.coreDataStack = coreDataStack
return true
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "fetchedResultsController" {
print ("Gold")
let a = window?.rootViewController as? UITabBarController
let employeeListNavigationController = a?.viewControllers?[0] as? UINavigationController
let b = employeeListNavigationController?.topViewController as? EmployeeListViewController
print( b?.fetchedResultsController.fetchedObjects?.count)
}
}
it showed me that it was nil then right away 50. and apparently they are also faults. Then why do we need fetchBatchSize and when it comes to play? And how? Please if someone have any idea i would appreciate very much