后台任务使用以下命令杀死应用-调试器的消息:由于信号9而终止

时间:2018-10-11 16:34:36

标签: swift memory-management sigkill uibackgroundtask

我要从Firebase Function下载文本文件,分析该文件中的数据,然后将数据插入我的应用程序中的sqlite中。我在那个文本文件中有超过1,000,000条记录。当应用程序处于前台模式时,这可以正常工作。我想在应用程序处于后台模式时完成此任务,但是当应用程序进入后台时,解析和插入被暂停。

这是我正在使用的代码,

WITH cte AS 
(
    SELECT DISTINCT 
        Lab_Space_Id
    FROM 
        EHS_Assessment_Audit
    WHERE 
        Audit_Date >= DATEADD(year, -1, GETDATE())
)
SELECT 
    l.Site_Name, l.Campus_Name, 
    COUNT(DISTINCT s.id) Total,
    SUM(CASE WHEN a.Lab_Space_ID IS NOT NULL THEN 1 ELSE 0 END) Audited
FROM
    Lab_Space s
LEFT OUTER JOIN 
    cte a ON s.id = a.Lab_Space_Id
JOIN 
    Locations l ON l.Building_Code = s.Building_Code
GROUP BY 
    l.Site_Name, l.Campus_Name
ORDER BY 
    l.Site_Name, l.Campus_Name

但是,我的backgroundTask在几秒钟后暂停了。如果我删除了var backgroundDownload3 : UIBackgroundTaskIdentifier? func downloadBook(at indexPath : IndexPath) { backgroundDownload3 = UIApplication.shared.beginBackgroundTask(expirationHandler: { if let task = backgroundDownload3 { UIApplication.shared.endBackgroundTask(task) backgroundDownload3 = UIBackgroundTaskIdentifier.invalid } }) DispatchQueue.global().async { self.fetchInteractionData(at: indexPath) } } func fetchInteractionData(at indexPath : IndexPath){ //Firebase Function to download textFile //download completed downloadTask.observe(.success) { snapshot in self.parseAndSaveToDataBase(fileName : name, at: indexPath) } } func parseAndSaveToDataBase(fileName : String, at indexPath : IndexPath) { //Parsing data from text file //batch Insert (500 records) with db.transaction // Once all the record are inserted if let task = backgroundDownload3 { UIApplication.shared.endBackgroundTask(task) backgroundDownload3 = UIBackgroundTaskIdentifier.invalid } } ,它会继续在后台运行,但是一段时间后,应用会被expirationHandler endBackgroundTask杀死

我看着类似的问题,才知道这可能是内存或CPU使用率高。

我的CPU每次使用率约为95%,当应用程序被杀死时,内存使用率介于40 MB到80 MB之间。这是在后台发生的唯一过程。我不确定如何才能继续在后台模式下解析和插入数据。如何减少CPU或内存使用量或延长后台运行时间。请让我知道是否需要进一步澄清。任何帮助将不胜感激。

0 个答案:

没有答案
相关问题