Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 应用程序在具有后台提取的Collectionview的reloadData()时崩溃_Swift_Uicollectionview_Background Fetch - Fatal编程技术网

Swift 应用程序在具有后台提取的Collectionview的reloadData()时崩溃

Swift 应用程序在具有后台提取的Collectionview的reloadData()时崩溃,swift,uicollectionview,background-fetch,Swift,Uicollectionview,Background Fetch,因此,我正在进行后台提取,并且在我想要更新UI之后。后台抓取本身(数据库)工作正常,但当我想更新UI时,在展开可选值时,我意外地遇到了一个崩溃发现了nil 根据我的xCodeself,newestPublicationsCollectionView.reloadData()是updateCollections()函数中发生崩溃的地方 func updateCollections() { // get latest data from Database lates

因此,我正在进行后台提取,并且在我想要更新UI之后。后台抓取本身(数据库)工作正常,但当我想更新UI时,在展开可选值时,我意外地遇到了一个崩溃
发现了nil
根据我的xCode
self,newestPublicationsCollectionView.reloadData()是updateCollections()函数中发生崩溃的地方

 func updateCollections() {
        // get latest data from Database
        latestPublications = getNewestPublications()

        self.newestPublicationsCollectionView.reloadData()

        self.newestPublicationsCollectionView.layoutIfNeeded()

       // fix wrong content height
       self.newestPublicationsCollectionViewHeight.constant = self.newestPublicationsCollectionView.collectionViewLayout.collectionViewContentSize().height  
    }
AppDelegate中的其他代码:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    let fetchViewController = PublicationOverviewController()
                fetchViewController.fetch {
                    dispatch_async(dispatch_get_main_queue()) {
                    fetchViewController.updateCollections()
                    }
                    completionHandler(.NewData)
                }
}
请输入我的出版物OverviewController

func fetch(completion: () -> Void) {
    PublicationFetcher.shared.fetchAllPublications()
    completion()
}
我以为崩溃是因为我需要主线程上的UI,但那没有帮助。任何输入都很好

详细信息:


在我的PublicationFetcher.shared.fetchAllPublications()中,我执行以下操作:

  • 从后端获取数据
  • dispatch\u async(dispatch\u get\u main\u queue()){()->NSNotificationCenter.defaultCenter().postNotificationName(“重载通知”,对象:nil)中的Void
在重新加载通知下,我执行一个
updateCollections()

Swift 3
在开始UI操作之前,请尝试在后台提取时给应用程序30秒的超时时间,这对您的后台网络连接很有帮助:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    let fetchViewController = PublicationOverviewController()
                fetchViewController.fetch {
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue()) {
                    <#code to be executed after a specified delay#>
                    fetchViewController.updateCollections()
                    completionHandler(.NewData)
                }
}
将其称为以下示例:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

        let fetchViewController = PublicationOverviewController()
                    fetchViewController.fetch {
                        dispatchDelay(25) { // put what you want
                            // this is main-thread queue
                            fetchViewController.updateCollections()
                            completionHandler(.NewData)
                        }
                    }
    }

在开始UI操作之前,请尝试在后台提取时给应用程序30秒的超时时间,这对您的后台网络连接很有帮助:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    let fetchViewController = PublicationOverviewController()
                fetchViewController.fetch {
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue()) {
                    <#code to be executed after a specified delay#>
                    fetchViewController.updateCollections()
                    completionHandler(.NewData)
                }
}
将其称为以下示例:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

        let fetchViewController = PublicationOverviewController()
                    fetchViewController.fetch {
                        dispatchDelay(25) { // put what you want
                            // this is main-thread queue
                            fetchViewController.updateCollections()
                            completionHandler(.NewData)
                        }
                    }
    }

不要再次初始化
PublicationOverviewController()
,因为它将为您提供该视图的新实例,并且您将无法获得引用的
collectionView
。此外,在
viewDidLoad:
get调用之前,您将获得所有UI控件nil

例如:

let *controller : PublicationOverviewController = PublicationOverviewController()
// controller.collectionView will be nil here because view hasn't loaded yet

您可以在
ApplicationIDBecomeActive:
方法中发布通知,并在
PublicationOverviewController
中观察它,然后在相应的方法中重新加载您的
collectionView

不初始化
PublicationOverviewController()
再次,因为它将为您提供该视图的新实例,而您将无法获得引用的
collectionView
。此外,在
viewDidLoad:
get调用之前,您将获得所有UI控件nil

例如:

let *controller : PublicationOverviewController = PublicationOverviewController()
// controller.collectionView will be nil here because view hasn't loaded yet


您可以在
ApplicationIDBecomeActive:
方法中发布通知,并在
PublicationOverviewController
中观察它,然后在相应的方法中重新加载您的
collectionView

您必须进入主线程才能更新UI,这没关系。但这意味着某处有一个值为零。试着找出哪一个。它说my
self.newestPublicationsCollectionView.reloadData()
为零,而它不是(当应用程序打开时显示数据)出现时为
newestPublicationsCollectionView
nil?当应用程序处于后台模式时,您不能更新UI。保存数据并在
applicationIDBecomeActive:
方法中执行。因为在后台模式下,每个UI控件/IBOutlets都将为零。@程序员已经尝试过了,但仍然崩溃,返回的结果是所有视图和按钮等都为零。有没有可能因为我的PublicationOverviewController位于tabbarview控制器中而返回nil?您必须进入主线程才能更新UI,这没关系。但这意味着某处有一个值为零。试着找出哪一个。它说my
self.newestPublicationsCollectionView.reloadData()
为零,而它不是(当应用程序打开时显示数据)出现时为
newestPublicationsCollectionView
nil?当应用程序处于后台模式时,您不能更新UI。保存数据并在
applicationIDBecomeActive:
方法中执行。因为在后台模式下,每个UI控件/IBOutlets都将为零。@程序员已经尝试过了,但仍然崩溃,返回的结果是所有视图和按钮等都为零。有没有可能因为我的PublicationOverviewController位于tabbarview控制器中而返回nil?您好,谢谢您的回答,但现在崩溃只是延迟了(但仍然发生)。我将给出我的updateCollections()函数中发生的事情的完整版本,也许这会有帮助,所以现在我们必须排除对主线程的过快尝试,现在我检查您的更新..在我的PublicationFetcher.shared.fetchAllPublications()中,我执行以下操作(1.从后端获取数据,2.
dispatch\u async(dispatch_get_main_queue()){()->Void in NSNotificationCenter.defaultCenter().postNotificationName(“RELOAD_NOTIFICATION”,object:nil)}
在该重载通知下执行一个
updateCollections()
。也许它们之间存在冲突?当然,您必须删除它。对updateCollections()的调用太多,在这里的评论中,您不尊重performFetchWithCompletionHandler延迟,但对我来说,仍然实现
fetchViewController.updateCollections()是否仍然有用
?不应该单独执行.fetch来解决问题吗?(不确定)嗨,谢谢你的回答,但是现在崩溃只是延迟了(但它仍然发生)。我将在我的updateCollections()中给出发生的事情的完整版本函数,可能这会有帮助,所以现在我们必须排除对主线程的过快尝试,现在我检查您的更新..在我的PublicationFetcher.shared.fetchAllPublications()中,我执行以下操作(1.从后端获取数据,2.
dispatch_async(dispatch_get_main_queue()){()->Void In NSNotificationCenter.defaultCenter().postNotificationName(“RELOAD_NOTIFICATION”,object:nil)}
在该重新加载通知下,我执行了一个
updateCollections()
。可能它们之间存在冲突?当然,您必须删除它。对updateCollections()的调用太多了在这篇评论中,你不尊重performFetchWithCompletionHandler Delays,但对我来说,仍然导入