如何在后台线程中加载核心数据
how to load the Core Data in background thread
我的核心数据存储中有一些大数据
我如何在后台线程中加载此数据?
func connectionCoreData() {
let fetchRequest = NSFetchRequest<PersonalClass>(entityName: "PersonalBase")
let sortDescriptor = NSSortDescriptor(key: "personName", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let managerObjectContext = (UIApplication.shared.delegate as? AppDelegate)?.managedObjectContext {
fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managerObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
do {
try fetchResultController.performFetch()
personalArray = fetchResultController.fetchedObjects!
self.tableView.reloadData()
} catch {
print(error)
}
}
}
我需要在后台线程中添加核心数据加载,然后更新我的 tableView
首先,您应该记住 managedObjectContext 在单个线程上运行。你应该 access/edit 加载的对象在同一个线程上。
在您的情况下,您将与要在主线程上加载的对象进行交互。例如,加载的数据库对象将填充 tableView
,这应该在主线程上完成。这会强制 managedObjectContext 成为在主线程上运行的 MainContextType。
你不应该害怕 运行 主线程上的 NSFetchedResultsController,因为它会批量加载对象。但是,您没有按应有的方式使用 FetchedResults 控制器。你的代码中不应该有这两行。
personalArray = fetchResultController.fetchedObjects!
self.tableView.reloadData()
您应该使用此方法访问加载的对象fetchResultController .objectAtIndexPath(indexPath)
。
这是一个如何使用 NSFetchedResultsController
的示例
class ViewController: UITableViewController NSFetchedResultsControllerDelegate{
lazy var fetchedResultsController: NSFetchedResultsController = {
let fetchRequest = ... //Create the fetch request
let sortDescriptor = ... //Create a sortDescriptor
let predicate = ...//Create the predicate if you want to filter the results
fetchRequest.predicate = predicate
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: mainContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
override fun viewDidLoad(){
super.viewDidLoad()
do {
try self.fetchedResultsController.performFetch()
}catch {
print(error)
}
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch (type) {
case .Insert:
if let indexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
break;
case .Delete:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
break;
case .Update:
if let indexPath = indexPath {
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
break;
case .Move:
if let indexPath = indexPath, newIndexPath = newIndexPath {
tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath)
}
break;
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController.sections {
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
return 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let obj = fetchedResultsController.objectAtIndexPath(indexPath){
....
}
}
我的核心数据存储中有一些大数据 我如何在后台线程中加载此数据?
func connectionCoreData() {
let fetchRequest = NSFetchRequest<PersonalClass>(entityName: "PersonalBase")
let sortDescriptor = NSSortDescriptor(key: "personName", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let managerObjectContext = (UIApplication.shared.delegate as? AppDelegate)?.managedObjectContext {
fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managerObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
do {
try fetchResultController.performFetch()
personalArray = fetchResultController.fetchedObjects!
self.tableView.reloadData()
} catch {
print(error)
}
}
}
我需要在后台线程中添加核心数据加载,然后更新我的 tableView
首先,您应该记住 managedObjectContext 在单个线程上运行。你应该 access/edit 加载的对象在同一个线程上。
在您的情况下,您将与要在主线程上加载的对象进行交互。例如,加载的数据库对象将填充 tableView
,这应该在主线程上完成。这会强制 managedObjectContext 成为在主线程上运行的 MainContextType。
你不应该害怕 运行 主线程上的 NSFetchedResultsController,因为它会批量加载对象。但是,您没有按应有的方式使用 FetchedResults 控制器。你的代码中不应该有这两行。
personalArray = fetchResultController.fetchedObjects!
self.tableView.reloadData()
您应该使用此方法访问加载的对象fetchResultController .objectAtIndexPath(indexPath)
。
这是一个如何使用 NSFetchedResultsController
class ViewController: UITableViewController NSFetchedResultsControllerDelegate{
lazy var fetchedResultsController: NSFetchedResultsController = {
let fetchRequest = ... //Create the fetch request
let sortDescriptor = ... //Create a sortDescriptor
let predicate = ...//Create the predicate if you want to filter the results
fetchRequest.predicate = predicate
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: mainContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
override fun viewDidLoad(){
super.viewDidLoad()
do {
try self.fetchedResultsController.performFetch()
}catch {
print(error)
}
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch (type) {
case .Insert:
if let indexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
break;
case .Delete:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
break;
case .Update:
if let indexPath = indexPath {
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
break;
case .Move:
if let indexPath = indexPath, newIndexPath = newIndexPath {
tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath)
}
break;
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController.sections {
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
return 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let obj = fetchedResultsController.objectAtIndexPath(indexPath){
....
}
}