我有一个应用程序,它在后台模式下工作,音乐在后台模式下播放,在播放音乐背景时,我们调用 API 并将 10,000 条记录数据解析到本地数据库中。但它没有发生,似乎应用程序在后台崩溃。
欢迎任何建议,如果我们没有点击任何 API 并且音乐正在后台播放,但是当 API 解析发生时,音乐和应用程序将停止。
guard let bundle = Bundle.main.path(forResource: "bird", ofType: "wav") else{return}
let alertSound = URL(fileURLWithPath: bundle)
//try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.moviePlayback, options: AVAudioSession.CategoryOptions.mixWithOthers)
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.moviePlayback, policy: AVAudioSession.RouteSharingPolicy.default, options: AVAudioSession.CategoryOptions.mixWithOthers)
//try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
try self.player = AVAudioPlayer(contentsOf: alertSound)
// Play audio forever by setting num of loops to -1
self.player?.numberOfLoops = -1
self.player?.volume = 0.01
self.player?.prepareToPlay()
self.player?.play()
最佳答案
在后台模式下你能做的事情非常有限。您可以使用后台应用程序刷新来完成有限的任务 apple docs for app refresh和 app background lifecycle method .在线有很多关于后台应用程序刷新的资源。如果用户禁用应用程序的后台应用程序刷新,即使这样也不起作用。
关于ios - 如何在后台运行 iOS 应用程序来解析海量数据 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58922092/