【发布时间】:2017-12-13 17:11:18
【问题描述】:
今天做简单测试的时候
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
}
我想知道 fetchBachSize 真的有效吗?我试图查看调试部分是完整的数组(应该是 50 个元素)。所有这些都是错误的。行。然后我尝试为 FetchedResultsController 的 fetchedObject 的计数属性添加观察者
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)
}
}
它告诉我它是零,然后马上 50。显然它们也是错误的。那么为什么我们需要 fetchBatchSize 以及何时播放?如何?如果有人有任何想法,我将不胜感激
【问题讨论】: