srinadh
srinadh

Reputation: 123

How to run iOS app in background to parse huge data API?

I have an application, it works in background mode and music is playing in background mode and while playing music background, we call API and parse 10,000 records data into local db. but it is not happening, Seems app crashing in the background.

Any suggestion are appreciated, If we are not hit any API and music is playing in the background but when API parsing happens, music and app are going to stop.

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()

Upvotes: 1

Views: 256

Answers (1)

Samin
Samin

Reputation: 273

There are very limited things you can do on background mode. You can use background app refresh to do limited task apple docs for app refresh and app background lifecycle method. There are great resources online on background app refresh. Even that will not work if user disabled background app refresh for the app.

Upvotes: 0

Related Questions