첫 번째 업데이트 후 백그라운드 스레드에서 핵심 데이터로드를 추가해야합니다. 그리고 같은 스레드에서로드 된 객체를 액세스/편집해야합니다.
경우에 따라 주 스레드에서로드 할 개체와 상호 작용할 수 있습니다. 예를 들어,로드 된 데이터베이스 개체는 tableView
으로 채우며 주 스레드에서 수행해야합니다. 이렇게하면 managedObjectContext가 주 스레드에서 실행되는 MainContextType이됩니다.
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html
당신은 일괄 적으로 개체를로드하기 때문에 메인 스레드에서 NSFetchedResultsController를 실행 두려워해서는 안된다. 그러나 FetchedResults Controller를 사용해야하는 것은 아닙니다. 코드에이 두 줄을 사용하면 안됩니다.
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){
....
}
}
감사를 사용하는 방법의 예입니다,하지만 난 (NSFetchedResultsController의 기본로드를 핵심 데이터의 32K 행이있을 경우 몇 초 정도 걸립니다, 그리고 난에 검색을 추가 할 이 기본) 그리고 내가해야 할 일을 코어 데이터가 더 빨리로드해야합니까? 핵심 데이터에 더 많은 관계를 추가하고 그룹별로 관계를 통해 기반을로드하려고 시도 할 수 있습니까? 아니면이 시간도 걸릴 수 있습니까? – Skie
정말 당신이 무엇을하려고하는지에 달렸습니다 ... 당신은 쿼리를 최적화하려고 노력해야합니다. 색인 생성을 사용하십시오. 그러나 NSFetchedresultscontroller를 백그라운드 컨텍스트에 두는 것은 악몽입니다. 왜냐하면 그렇게 사용하지 않으려 고하기 때문입니다. – ELKA