ios - 如何在后台运行 iOS 应用程序来解析海量数据 API?

标签 ios swift background avaudioplayer

我有一个应用程序,它在后台模式下工作,音乐在后台模式下播放,在播放音乐背景时,我们调用 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 refreshapp background lifecycle method .在线有很多关于后台应用程序刷新的资源。如果用户禁用应用程序的后台应用程序刷新,即使这样也不起作用。

关于ios - 如何在后台运行 iOS 应用程序来解析海量数据 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58922092/

相关文章:

swift - 即使设备支持 Apple Pay,Apple Pay 按钮也不会显示

ios - 当我的应用程序在 ios 后台终止时如何执行代码

安卓编程 : Background color not fully covering fragments

iphone - 如何从 iPhone 音乐库中获取特定年份的所有歌曲?

ios - 导航栏后面的搜索栏

ios - 一旦出现 uicontroller 扫描蓝牙设备

ios - NSFileManager fileExistsAtPath 无法覆盖 Swift 2 中的现有文件

ios - Swift:调用 didDeselectAnnotationView 后,MKMapView 注释出现在左上角

ios - 更新 UILabel Swift

iphone - 从后台恢复应用程序会重新启动整个应用程序吗?