快速加载核心数据后异步刷新UITableView [英] Refreshing UITableView Asynchronously after Core Data Loaded Swift

查看:63
本文介绍了快速加载核心数据后异步刷新UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,tViewNews

I have a UITableView, tViewNews

我具有刷新功能,该功能可以从服务器下载数据,并将其存储到核心数据中,然后表视图从核心数据中加载该数据.

I have a refresh function which downloads data from my server, stores it into core data, and then the table view loads this data from core data.

效果很好

func refresh(refreshControl: UIRefreshControl) {

    self.newsArray = [NewsItem]()
    self.newslists = [[NewsItem]]()
    self.getNewsFromServer()
    self.getNewsFromCoreData()
    self.tViewNews.reloadData()

    refreshControl.endRefreshing()
}

现在,当用户首次打开新闻viewDidLoad()时,我希望表视图首先从核心数据加载,然后从服务器异步填充我的表视图数组(使用与刷新完全相同的方法)函数),然后重新加载表格视图.

Now, when the user first opens the news viewDidLoad(), I would like for the table view to first load from the core data, then asynchronously populate my table view array from the server (using precisely the same method as the refresh function) and then reload the table view.

所以我在viewDidLoad()函数中尝试了以下代码.

So I have tried the following code in my viewDidLoad() function.

override func viewDidLoad() {
    super.viewDidLoad()

    // Getting news from Core Data
    getNewsFromCoreData()

    // Updating core data from server and populating table view from core data
    dispatch_async(dispatch_get_main_queue()) {

        self.newsArray = [NewsItem]()
        self.newslists = [[NewsItem]]()
        self.getNewsFromServer()
        self.getNewsFromCoreData()
        self.tViewNews.reloadData()

    }

如您所见,异步请求中运行的功能与刷新功能中的功能相同.

As you can see the functions being run inside the async request are identical to those inside the refresh function.

但是,刷新功能可以正常运行,填充并重新加载表格视图.但是异步请求绝对不执行任何操作.表格视图保持为空.但是,这些功能正在运行,已经通过打印语句进行了测试.

But!, the refresh function functions properly, populating and reloading the table view. But the async request does absolutely nothing. The table view remains empty. But the functions are running, as tested with print statements.

有人知道为什么会这样吗?

Anyone know why this is the case?

编辑-添加了更多功能

获取核心数据

func getNewsFromCoreData() {
    let temp = StaffCoreData()
    temp.getAllNews()

    newsDates = Dictionary<Int, [NewsItem]>()

    for newsobj in temp.newsArray{

        if var curdate = newsDates[toNewsItem(newsobj).age]{

            curdate.append(toNewsItem(newsobj))
            newsDates[toNewsItem(newsobj).age] = curdate


        }else{

            newsDates[toNewsItem(newsobj).age] = [toNewsItem(newsobj)]

        }
    }

    for var i = 0; i<50; ++i{if let curdate = newsDates[i]{newslists.append(curdate)}}

获取服务器数据

使用以下方法运行实例:

runs instance with following method:

func NewsCallReturnsWithSuccess(data: NSData) {

    if let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) {
        if let statusesArray = jsonObject as? NSArray{

            newsArray.removeAll(keepCapacity: true)

            for item in statusesArray {
                let datacon : NSData = item.dataUsingEncoding(NSUTF8StringEncoding)!
                let jsonObject : NSDictionary! = NSJSONSerialization.JSONObjectWithData(datacon, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

                let title : NSString = jsonObject.objectForKey("title") as! NSString
                let content : NSString = jsonObject.objectForKey("content") as! NSString
                let publishedby : NSString = jsonObject.objectForKey("publishedby") as! NSString
                let modified : NSString = jsonObject.objectForKey("modified") as! NSString
                let imageurl : NSString = jsonObject.objectForKey("imageurl") as! NSString
                let category : NSString = jsonObject.objectForKey("category") as! NSString

                let newsItem = NewsItem(title: title as String, content: content as String, category: category as String, imageURL: imageurl as String, publishedBy: publishedby as String, modified: modified as String)

                newsArray.append(newsItem)
                }

            //add complete array to CoreData
            let temp = StaffCoreData()
            temp.addArrayOfNew(newsArray)

        }

    }

}

推荐答案

在使用其他线程加载数据时,在完成加载时必须返回主线程以更新UI.

When you are using a different thread for loading your data, you have to return to the main thread when finished loading to update the UI.

赞:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        // load data
        dispatch_async(dispatch_get_main_queue()) {
            // update ui
        }
    }

更新

请向我们展示您的getNewsFromCoreData()getNewsFromServer()方法,也许还有您的cellForRowAtIndexPath.

Pls show us your getNewsFromCoreData() and getNewsFromServer() methods and maybe your cellForRowAtIndexPath, too.

override func viewDidLoad() {
    super.viewDidLoad()

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        // load data
        self.newsArray = [NewsItem]()
        self.newslists = [[NewsItem]]()
        self.getNewsFromServer()
        self.getNewsFromCoreData()
        dispatch_async(dispatch_get_main_queue()) {
            // update ui
            self.tViewNews.reloadData()
        }
    }
}

这篇关于快速加载核心数据后异步刷新UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆