【发布时间】:2015-02-12 06:56:22
【问题描述】:
我正在从博客中获取 json 数据并将其存储在核心数据中。现在我想添加拉刷新,这样如果博客上有任何更新,它们也会反映在应用程序中。我发现了一个类似问题的帖子,它建议添加此代码
override func viewDidLoad() {
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to Refresh")
refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
}
func refresh() {
NSFetchedResultsController.deleteCacheWithName("Master")
var error: NSError? = nil
self.fetchedResultsController.performFetch(&error)
self.tableView.reloadData()
println("refreshed")
}
问题是下拉刷新后coredata和tableview都没有更新。
核心数据中获取和保存json数据的代码如下:
func animalSelected(animal: Animal) {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
let session = NSURLSession.sharedSession()
var error : NSError?
let task = session.dataTaskWithURL(animal.url!, completionHandler: {data, response, error -> Void in
if (error != nil){
println(error)
}else{
var request = NSFetchRequest(entityName: "MIBlog")
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
for result in results!
{
context.deleteObject(result as NSManagedObject)
context.save(nil)
}
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
var posts = [[String:String]()]
var post:AnyObject
var authorDictionary:AnyObject
var newBlogItem:NSManagedObject
for var i = 0; i < jsonResult["posts"]!.count; i++
{
posts.append([String:String]())
post = jsonResult["posts"]![i] as NSDictionary
posts[i]["title"] = post["title"] as? NSString
posts[i]["publishedDate"] = post["date"] as? NSString
posts[i]["content"] = post["content"] as? NSString
authorDictionary = post["author"] as NSDictionary
posts[i]["author"] = post["name"] as? NSString
newBlogItem = NSEntityDescription.insertNewObjectForEntityForName("MIBlog", inManagedObjectContext: context) as NSManagedObject
newBlogItem.setValue(posts[i]["title"], forKey: "title")
newBlogItem.setValue(posts[i]["publishedDate"], forKey: "publishedDate")
newBlogItem.setValue(posts[i]["content"], forKey: "content")
newBlogItem.setValue(posts[i]["author"], forKey: "author")
context.save(nil)
}
request = NSFetchRequest(entityName: "MIBlog")
request.returnsObjectsAsFaults = false
results = context.executeFetchRequest(request, error: nil)
}
})
task.resume()
delegate?.collapseSidePanels?()
}
我的 fetchResultController 代码:
// MARK: - Fetched results controller
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
self.managedObjectContext = appDel.managedObjectContext
let fetchRequest = NSFetchRequest()
// Edit the entity name as appropriate.
let entity = NSEntityDescription.entityForName("MIBlog", inManagedObjectContext: self.managedObjectContext!)
fetchRequest.entity = entity
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "publishedDate", ascending: false)
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = [sortDescriptor]
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//println("Unresolved error \(error), \(error.userInfo)")
abort()
}
return _fetchedResultsController!
}
var _fetchedResultsController: NSFetchedResultsController? = nil
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath)!, atIndexPath: indexPath)
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
default:
return
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
activityIndicator.stopAnimating()
}
我不知道在刷新时应该如何更新核心数据。
请帮忙。
【问题讨论】:
标签: swift core-data nsfetchedresultscontroller pull-to-refresh uirefreshcontrol