繁体   English   中英

Swift值在闭包内消失了

[英]Swift value disappeared inside a closure

我有以下代码:

    let context = DBHelper.privateContext()
    context.performBlock({ [weak self] in
        if let ss = self {
            //1. 
            let articles = DBHelper.clientFetchArticles(context) 
            //2. 
            dispatch_async(dispatch_get_main_queue(), {
                //3. 
                ss.articles = articles
                ss.tableView.reloadData()
                ss.refreshControl.endRefreshing()
            })
        }
    })

调用tableView.reloadData() ,运行以下代码:

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

    #4
    let article = articles[indexPath.row]


    cell.setupWithArticle(article)

    return cell
}

DBHelper.clientFetchArticles将返回[Article] 2篇文章( DBHelper.clientFetchArticles类型)。

  • 在#1,当我打印po articles[0].title ,我打印出正确的title

  • 在#3, po articles.count打印2po articles[0].title也是正确的

  • 在#4, po self.articles[0].title得到零。

我尝试过dispatch_sync(dispatch_get_main_queue())但结果仍然相同。

这可能是因为你的privateContext()实现。 执行回调后,将释放捕获的context ,这将使从该上下文获取的Article对象无效。

建议的约定是在处理UI获取请求时使用“主要上下文”(从AppDelegate一直传递的那个)。 使用private context进行异步更新(例如,networing)。

我的猜测是dispatch_async闭包是捕获它周围的值但是因为你没有修改捕获的值,在本例中是articles ,它会复制它而不是对该数组的引用。

简单的解决方法是使articles成为该类的属性

链接到Swift文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM