Mutli-threaded给出的结果无序

时间:2016-07-19 17:23:59

标签: swift multithreading

我对多线程很陌生。它产生了一个我不期待的结果,但是当我想到它时它才有意义。现在我需要找到一种方法让它正常工作。

这是场景......

我有例程,调用带有请求日期的网页。我们称之为getPage(inDate:String)。结果是未知的,但大小不同,具体取决于提交的日期。 (今天可以有100个项目,明天有2个项目,第二天有10个项目。)

我有一个在日期序列中调用getPage的循环。

每次返回都是tableview中的一个部分。

PSEUDO CODE 

func viewDidLoad() {   
    ...


    for cntr in 1...4 {
        getPage(calculatedDate)
    }

    ...
}

getPage(inDate: String) {
    let task = NSURLSession ( ....) {

    ...

    //  create tableview section data using self. arrays of string
    dispatch_sync(dispatch_get_main_queue(), { () -> Void in
        self.tableView.reloadData()
    })


}
   override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.dateSection.count // array of sections
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataItem1[section].count // an array of data for the section
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let mycell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! calCell

    self.navigationItem.title = "Calendar Data"

    // Configure the cell...
    mycell.dataItem1.text =  self.data1[indexPath.section][indexPath.row]
    mycell.dataItem2.text = self.data2[indexPath.section][indexPath.row]
    mycell.dataItem3.text = self.data3[indexPath.section][indexPath.row]
    mycell.dataItem4.text = self.data4[indexPath.section][indexPath.row]

    return mycell

}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    //        print("In titleForHederInSection:  Section \(section)")
    //        print("Title: \(self.dateSection[section])")
    return self.dateSection[section]
}

在我放置reload()的地方似乎并不重要,或者如果我使用dispatch_sync或dispatch_async,表格无序......首先给我最快的回报。

我对信号量的初步尝试是一个令人沮丧的失败......永远锁定应用程序。

所以问题是,如何按照我调用网页的顺序(在这种情况下,日期顺序)构建视图表?

0 个答案:

没有答案
相关问题