V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
HannibaI
V2EX  ›  iDev

在构造函数里调用 URLSession.shared.dataTask(), 在 dataTask()参数的闭包里给类成员赋值的问题?

  •  
  •   HannibaI ·
    ingjieye · 2016-11-25 09:51:54 +08:00 · 4095 次点击
    这是一个创建于 2681 天前的主题,其中的信息可能已经有所发展或是发生改变。

    程序打开是一个 UITableView, 在 TableViewController 的 viewDidLoad()函数中构造类 Series 的对象并保存在外部数组中。

    class SeriesTableViewController: UITableViewController {
    
        var seriesArray = [Series]()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            loadSampleSeries()
        }
        
        func loadSampleSeries() {
            let photo1 = UIImage(named:"WestWorld")
            let name1 = "西部世界  WestWorld"
            let url1 = "http://www.zimuzu.tv/resource/index_json/rid/33701/channel/tv"
            let series1 = Series(name: name1, photo: photo1, zimuzuJsonUrl: url1)
            
            seriesArray.append(series1)
        }
        ... ...
    }
    

    Series 的构造函数会调用 URLSession.shared.dataTask(), 在 dataTask 的闭包中对返回的 data 进行解析然后赋值给类 Series 的成员。

    class Series {
    
        var nextDate: String = ""
        var nextTime: Int = 0
        ...
        ...
        init( ... ... ) {
            let task = URLSession.shared.dataTask(with: request) {
                (data, response, error) -> Void in
                ...
                ...
                 self.nextTime = ...
                 self.nextDate = ...
                }
            }
            task.resume()
        }
        ...
        ...
    }
    

    但是发现 URLSession.shared.dataTask 并没有在构造函数中马上被执行,直到 UITableViewController 调用了 UITableViewDataSource 后才执行并对 Series 的对象赋值。导致 TableView 中无法显示正确的数据。

    在控制台打印发现各个函数的执行顺序是这样的:

    didFinishLaunchingWithOptions
    viewDidLoad()
    Series.init()
    tableView() -> UITableViewCell
    URLSession.shared.dataTask()
    

    问题是:

    • 为什么我在 Series.init()中调用的 URLSession.shared.dataTask()没有马上执行?
    第 1 条附言  ·  2016-11-25 18:52:26 +08:00
    问题解决 谢谢 2#
    4 条回复    2016-11-30 16:31:31 +08:00
    HannibaI
        1
    HannibaI  
    OP
       2016-11-25 12:02:48 +08:00
    好吧 不知道这个是不是答案

    loveuqian
        2
    loveuqian  
       2016-11-25 12:14:50 +08:00 via iPhone   ❤️ 1
    那是一个异步 block 吧

    可能你需要在 block 里 reload 一下 table ?
    HannibaI
        3
    HannibaI  
    OP
       2016-11-25 18:51:50 +08:00
    @loveuqian 好吧。学了好几天第一次接触到 block 的概念。我还一直以为那是个闭包。谢谢!
    dsmo
        4
    dsmo  
       2016-11-30 16:31:31 +08:00   ❤️ 1
    swift 里叫闭包, oc 里是 block
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2442 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:51 · PVG 23:51 · LAX 08:51 · JFK 11:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.