NSFetchedResultsController를 UITableView에서 사용하려고합니다.하지만 새 항목을 추가 할 때 NSFetchedResultsControllerDelegate가 해고되지 않습니다. 나는 문맥에 항목을 추가하고 controllerWillChangeContent
도 controllerDidChangeContent
가 모두 호출되는 내용을 저장하면 여기에 전체 코드NSFetchedResultsControllerDelegate : controllerDidChangeContent가 iOS 8에서 작동하지 않습니다.
import UIKit
import CoreData
let coreDataStack = CoreDataStack()
class CDListViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fRequest = fetchRequest()
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fRequest, managedObjectContext: coreDataStack.managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
}
println(error)
return _fetchedResultsController!
}
var _fetchedResultsController: NSFetchedResultsController? = nil
func fetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Menu")
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 2
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
override func viewDidLoad() {
super.viewDidLoad()
fetchedResultsController.delegate = self
print(fetchedResultsController)
// fetchedResultsController?.performFetch(nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let menus = fetchedResultsController.fetchedObjects as [Menu]
return menus.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let curr = fetchedResultsController.objectAtIndexPath(indexPath) as Menu
// Configure the cell...
cell.textLabel?.text = curr.text
return cell
}
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
func controllerWillChangeContent(controller: NSFetchedResultsController) {
println("Coming in here")
self.tableView.beginUpdates()
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let entry = self.fetchedResultsController.objectAtIndexPath(indexPath) as Menu
coreDataStack.managedObjectContext?.deleteObject(entry)
coreDataStack.saveContext()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch(type) {
case NSFetchedResultsChangeType.Insert : self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
break
case NSFetchedResultsChangeType.Delete : self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
break
case NSFetchedResultsChangeType.Update : self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
default:
println("Nothing")
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch(type) {
case NSFetchedResultsChangeType.Insert : self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
break
case NSFetchedResultsChangeType.Delete : self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic)
break
default:
println("Nothing")
}
}
}
입니다. 애플 리케이션을 다시 시작하면 최근에 추가 된 항목을 볼 수 있기 때문에 컨텍스트가 저장되었는지 확인했습니다.
누구든지 위의 코드로 문제를 찾을 수 있습니까?
알아 냈어. 나는 여러 managedObject 컨텍스트를 가졌다. 단일 관리 객체 컨텍스트를 생성하면 문제가 해결됨 – Shrikar