Understanding Periodic Background Processing

Asked 2 years ago, Updated 2 years ago, 40 views

I am a beginner who started creating iOS apps on Swift.This is the level of experience that I had made PC software as a hobby in c language more than 10 years ago.

Currently, I would like to create an app that periodically accesses webAPI in the background (about every 5 minutes) to notify me of any updates to the information.

When I looked it up in my own way, I got the impression that it was as follows.

  • Background processing is not recommended by default because it does not load the battery
  • Background fetch is not suitable if iOS determines the processing interval, and it is not constant every time, so you want to process it at an interval of about 5 minutes
  • Use Silent Push notifications to start apps regularly?However, for Push notification implementation, the threshold seems quite high for beginners, such as server preparation.

Is it better to think that it is difficult to perform background processing periodically with a clear time?

I would appreciate it if you could give me a hint.

swift ios

2022-09-30 20:36

4 Answers

As for Objective-C,
There is a way to link timers (NSTimers) with location information.

Retrieving location information can work in the background, but
By working with the timer,
You will be able to do something every five minutes.

I think the blog article I wrote before will be helpful.
http://grandbig.github.io/blog/2013/09/27/location-nstimer/

I don't think it uses location information, so
It may be rejected, but why don't you try it?
(Swift will also have timer processing.)

I hope it will be of some help.


2022-09-30 20:36

I was told that the background fetch cannot control the interval and the minimum interval is too long.
How about using sleep in one background fetch process?

Specifically, enable background fetch and
Implement the following code into AppDelegate:
(Example is Objective-C, but Swift should be able to do the same.)

-(void) application:(UIApplication*) application performFetchWithCompletionHandler:(void(^)(UIBackgroundFetchResult result)) completionHandler
{
    while(YES) {
        NSLog(@"%s", __func__);
        // Periodic processing

        NSThread sleepForTimeInterval:5*60;
    }
}


To actually use it Clear any questions about whether the implementation is acceptable or not, and
I think you will need to control the next time the background fetch runs, such as interrupting the last loop.

I ran the sample application that I created in a hurry on the simulator, and found out of> Repeat the NSLog output every 5 minutes (including the first time) 6 times. I was able to confirm this behavior.


2022-09-30 20:36

Selfless, I found the following code.

Swift how to use NSTimer background?- Stack Overflow

class ViewController:UIViewController {
    varbackgroundTaskIdentifier—UIBackgroundTaskIdentifier?

    override func viewDidLoad(){
        super.viewDidLoad()
        US>backgroundTaskIdentifier=UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
            UIAApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier!)
        })
        vartimer=NSTimer.scheduledTimerWithTimeInterval (1, target:self, selector: "update", userInfo:nil, repeat:true)
    }

    func update(){
        println ("Something cool")
    }
}

If you try running on the iOS simulator, println will continue to run in the background.
The comment said "This appropriate only runs for 3 minutes on iOS 7+." but it remained running after an hour.

Strangely enough, I tried to embed NSTimer in the performFetchWithCompletionHandler of BackgroundFetch separately, but the process continues in the background as well.

This may also be rejected by Apple, but it's a little strange that the process continues in the background as long as you embed NSTimer into the process that shouldn't be allowed to be processed for a long time.This is simply because it is on iOS simulator, and if it doesn't work on the actual machine, I understand...

I would appreciate it if you could comment on these if you have any suggestions or knowledge about my misunderstanding.


2022-09-30 20:36

I tried the above background task and timer method on the actual machine, but the background process stopped after about 3 minutes.

While debugging in Xcode, it was running for at least 3 minutes.


2022-09-30 20:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.