我有一个UITableView,tViewNews
我有一个刷新功能,可以从我的服务器下载数据,将其存储到核心数据中,然后表视图从核心数据中加载这些数据。
完美无缺
func refresh(refreshControl: UIRefreshControl) {
self.newsArray = [NewsItem]()
self.newslists = [[NewsItem]]()
self.getNewsFromServer()
self.getNewsFromCoreData()
self.tViewNews.reloadData()
refreshControl.endRefreshing()
}
现在,当用户首次打开新闻viewDidLoad()
时,我希望表视图首先从核心数据加载,然后从服务器异步填充我的表视图数组(使用与刷新功能)然后重新加载表视图。
所以我在viewDidLoad()
函数中尝试了以下代码。
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()
}
正如您所看到的,在异步请求中运行的函数与刷新函数内的函数相同。
但是!,刷新功能正常运行,填充并重新加载表视图。但异步请求绝对没有任何作用。表格视图仍为空。但正如使用print语句测试的那样,函数正在运行。
任何人都知道为什么会这样吗?
编辑 - 增加了额外的功能
获取核心数据
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)}}
获取服务器数据
使用以下方法运行实例:
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)
}
}
}
答案 0 :(得分:2)
当您使用其他线程加载数据时,必须在完成加载后返回主线程以更新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
。
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()
}
}
}