Mam UITableView, tViewNews
Odświeżanie UITableView asynchronicznie po Rdzeń danych Loaded Swift
Mam funkcję odświeżania, który pobiera dane z mojego serwera, przechowuje ją w danych podstawowych, a następnie zobacz tabela wczytuje te dane z danymi rdzenia .
To działa doskonale
func refresh(refreshControl: UIRefreshControl) {
self.newsArray = [NewsItem]()
self.newslists = [[NewsItem]]()
self.getNewsFromServer()
self.getNewsFromCoreData()
self.tViewNews.reloadData()
refreshControl.endRefreshing()
}
Teraz, gdy użytkownik po raz pierwszy otwiera wiadomości viewDidLoad()
chciałbym do widoku tabeli do pierwszego obciążenia z danych podstawowych, następnie asynchronicznie zapełnić moim stole widok tablicy z serwera (używając dokładnie tej samej metody, co funkcja odświeżania), a następnie ponownie załaduj widok tabeli.
Tak próbowałem poniższy kod w mojej funkcji 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()
}
Jak widać funkcje uruchamiane wewnątrz żądania asynchronicznego są identyczne jak funkcje wewnątrz funkcji odświeżania.
Ale !, funkcja odświeżania działa poprawnie, wypełniając i ponownie ładując widok tabeli. Ale żądanie asynchroniczne nie robi absolutnie nic. Widok tabeli pozostaje pusty. Ale funkcje są uruchomione, tak jak testowano za pomocą instrukcji print.
Ktoś wie dlaczego tak jest?
EDIT - Dodano dodatkowe fucntions
Get Rdzeń danych
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)}}
Get Server Data
uruchamia instancję z następującą metodą:
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)
}
}
}
Jak działają funkcje getNewsFromServer i getNewsFromCoreData. Czy pobierają treść w wątku tła? – Sandeep