I want to index the part of content through background thread each time the user run the app, but when i pressing 1 time Home button in middle of background task and app goes to background , this message i received : "Terminated due to signal 9". i want to index part of content that is possible when app is in foreground mode but in background thread without crashing the app. My main problem is why the app killed through middle of task running in background thread, even that task will be only print string in console! how should i handle this scenario?
Asked
Active
Viewed 674 times
1
-
Sounds that App uses too much CPU time or memory, then System killed it. – Yun CHEN Oct 17 '17 at 08:49
-
@YunCHEN each time i index only part of content for example in scale of 320000 word, i index only 500 word, but through indexing each 500 word, when i press Home, it killed.. – Farzaneh Givehchianfar Oct 17 '17 at 08:54
-
Provide the source code for indexing, maybe we can find a certain problem, or improve the performance. – Yun CHEN Oct 17 '17 at 09:13
-
Did you try using `UIBackgroundTask` as noted below? – Ladislav Oct 17 '17 at 11:16
1 Answers
0
You would have to start a background task and end it when you are done with your work something like this
var bgTask: UIBackgroundTaskIdentifier?
Add bgTask as your objects property and then
let application = UIApplication.shared
bgTask = application.beginBackgroundTask(withName: "name", expirationHandler: {
self.endBackgroundTask()
})
DispatchQueue.global(qos: .background).async {[weak self] in
//do your stuff here
self?.endBackgroundTask()
}
Add endBackgroundTask() method
func endBackgroundTask() {
if bgTask != UIBackgroundTaskInvalid {
UIApplication.shared.endBackgroundTask(bgTask!)
bgTask = UIBackgroundTaskInvalid
}
}
You can read more about this here

Ladislav
- 7,223
- 5
- 27
- 31