无法刷新核心数据并使用UIRefreshControl在tableview中显示它

时间:2015-02-12 06:56:22

标签: swift core-data nsfetchedresultscontroller pull-to-refresh uirefreshcontrol

我从博客中获取json数据并将其存储在核心数据中。现在我想添加pull to refresh,这样如果博客上有任何更新,它们也会在应用程序中反映出来。我发现了一个类似问题的帖子,它建议添加此代码

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

我无法弄清楚如何在刷新时更新核心数据。

Plsss plss请帮助。

1 个答案:

答案 0 :(得分:1)

当您使用NSFetchedResultsController时,您只需要正确实施NSFetchedResultsControllerDelegate方法。然后你所要做的就是保存。

context.save(nil)

一些评论:

我认为保存在循环中并不是一个好主意。插入所有对象后最好保存在最后。

删除所有内容并重新插入所有内容并没有多大意义。相反,您应该只更改那些有变化的对象。

当完成块返回时,将丢弃变量results。目前还不清楚你想用这个获取结果完成什么。

如果实现了获取的结果控制器委托,则需要再次调用performFetch,或者只需将获取的结果控制器设置为nil并让它懒散重新创造自己。然后只需更新应从获取的结果控制器

自动获取数据的UI
self.tableView.reloadData()