gpt4 book ai didi

ios - 未调用 controllerDidChangeContent

转载 作者:可可西里 更新时间:2023-11-01 01:42:07 26 4
gpt4 key购买 nike

我的应用程序非常简单:一个 TableViewController 显示存储在 Core Data 中的内容。添加按钮以模态方式推送第二个 View ,这使我可以将内容保存到 Core Data 中。我正在使用 NSFetchedResultsController 来填充主 VC。如果我将内容保存到 Core Data 并再次显示主视图 Controller ,我的 tableView 不会刷新其数据。我认为这是因为未调用委托(delegate)方法。知道为什么吗?

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()

}

最佳答案

问题是我的 ManagedObjectContext 没有被正确传递。 println(context) 在 App Delegate 和 View Controller #3 之间返回了不同的内存地址。我没有找到解决此问题的正确方法,所以我只是在 View Controller 之间传递上下文。有正确的上下文修复它。

关于ios - 未调用 controllerDidChangeContent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28798422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com