我的应用非常简单:TableViewController显示存储在Core Data中的内容。添加按钮以模态方式推送第二个视图,这允许我将内容保存到Core Data中。我正在使用NSFetchedResultsController
来填充主VC。如果我将内容保存到Core Data并再次显示主视图控制器,我的tableView不会刷新其数据。我认为这是因为没有调用委托方法。知道为什么吗?
import UIKit
import CoreData
class MainTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var managedContext: NSManagedObjectContext!
var fetchedResultsController: NSFetchedResultsController!
lazy var coreDataStack = CoreDataStack()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setToolbarItems(toolBar().cons, animated: true)
self.navigationController?.setToolbarHidden(false, animated: true)
let fetchRequest = NSFetchRequest(entityName: "CDPodcast")
let sort = NSSortDescriptor(key: "artist", ascending: true)
fetchRequest.sortDescriptors = [sort]
managedContext = coreDataStack.context
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
var error: NSError? = nil
if (!fetchedResultsController.performFetch(&error)) {
println("Error: \(error?.localizedDescription)")
}
}
以下都不称为
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
println("change section")
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.reloadData()
println("begin updates")
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.reloadData()
println("end updates")
}
修改:更多代码
func saveToCoreData(name: String, artist: String, summary: String, feedURL: String, artworkURL: String, date: NSDate, episodes: [MWFeedItem]) {
self.managedContext = coreDataStack.context
let podcastEntity = NSEntityDescription.entityForName("CDPodcast", inManagedObjectContext: self.managedContext)
currentPodcast = CDPodcast(entity: podcastEntity!, insertIntoManagedObjectContext: self.managedContext)
currentPodcast.name = name
currentPodcast.artist = artist
currentPodcast.summary = summary
currentPodcast.feedURL = feedURL
currentPodcast.artworkURL = artworkURL
currentPodcast.date = date
let episodesEntity = NSEntityDescription.entityForName("CDEpisode", inManagedObjectContext: self.managedContext)
for var i = 0; i < episodes.count; i++ {
let episodesToSave = CDEpisode(entity: episodesEntity!, insertIntoManagedObjectContext: self.managedContext)
var episode: AnyObject = currentPodcast.episode.mutableCopy() as! NSMutableOrderedSet
println("There are \(episodes.count) items in the episodes array")
episodesToSave.title = episodes[i].title
episodesToSave.downloadURL = episodes[i].enclosures[0].valueForKey("url") as! String
episodesToSave.showNotes = episodes[i].summary
episode.addObject(episodesToSave)
currentPodcast.episode = episode.copy() as! NSOrderedSet
}
self.coreDataStack.saveContext()
}
答案 0 :(得分:2)
这个问题是我的ManagedObjectContext没有被正确传递。 println(context)
在App Delegate和View Controller#3之间返回了不同的内存地址。我没有找到一个正确的方法来解决这个问题所以我只是在视图控制器之间传递上下文。有正确的上下文修复它。