如何在iOS中管理后台连续任务? [英] How to manage background consecutive tasks in iOS ?

查看:85
本文介绍了如何在iOS中管理后台连续任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





到目前为止,我能找到的关于iOS中后台任务的所有文档几乎与实现单个任务有关。但是,如果要在后台重复任务怎么办?

我无法弄清楚如何管理这个,因为一旦任务完成,我听起来就是这样,

必须调用endBackgroundTask才能关闭上一个任务,并且必须调用beginBackgroundTaskWithExpirationHandler才能启动新任务。



这就是下面的代码应该做的但它不起作用:

- 只要我的应用程序是前台,它就可以工作(请注意,任务ID总是在增加,我认为它会保持为1,因为只有一个任务启动在某个时间,我猜,已经是问题了。

- 当我进入后台模式时,剩余时间正在减少,直到任务完成,但没有新任务开始。

我想这是因为没有更多的后台任务在运行,但事实是我需要在后台启动后台任务...

我认为这不是管理这个和nee的好方法帮助我们开始正确的方式。



提前致谢。





 #importAppDelegate.h

@implementation AppDelegate

UIBackgroundTaskIdentifier backgroundTask;
NSTimer * timer = nil;
bool processDone = false;


- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//在应用程序启动后覆盖自定义点。
backgroundTask = UIBackgroundTaskInvalid;
timer = [NSTimer scheduledTimerWithTimeInterval:(0.1)target:self selector:@selector(timerFunc)userInfo:nil repeats:YES];

backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}];
NSLog(@init:新任务开始(%d),backgroundTask);

dispatch_queue_t queue;
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue,^ {
processDone = false;
[self process];
});

返回YES;
}


- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@applicationDidEnterBackground);
}

- (void)timerFunc
{
double remaining = [UIApplication sharedApplication] .backgroundTimeRemaining;
if(剩余<1000.0)
NSLog(@背景 - >剩余时间=%。1f秒,剩余);
else
NSLog(@Foreground);

if(processDone)
{
NSLog(@processDone:endBackgroundTask(%d),backgroundTask);
if(backgroundTask!= UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}

NSLog(@processDone:beginBackgroundTaskWithExpirationHandler);
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}];
NSLog(@processDone:新任务开始(%d),backgroundTask);

dispatch_queue_t queue;
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue,^ {
processDone = false;
[self process];
});

}
}


- (无效)处理
{
sleep(5);
processDone = true;
}

@end

解决方案

Apple设计不需要后台任务。你应该牢记这一点。



一个很好的教程来自 Ray Wenderlich



另一种解决方案可以是通知

Hi,

All documentations I could find so far regarding background tasks in iOS is almost related to achieving a single task. But what if a task is to be repeated in background ?
I could not figure out how to manage this as it sounds to me that as soon as the task is finished,
endBackgroundTask has to be called to close the previous task and beginBackgroundTaskWithExpirationHandler has to be called for the new task to be started.

That's what the code below is supposed to do, but it does not work :
- as long as my application is foreground, it works (notice that task id is always increasing, where I thought it would stay to 1, as only one task is started at a given time, which is, I guess, already as issue).
- when I enter background mode, remaining time is descreasing until task is finished, but no new task is started.
I guess this is because there is thus no more background task running, but the truth is I need to start a backgound task while in background ...
I think this is not the good way in managing this and need some help in starting the right way.

Thanks in advance.


#import "AppDelegate.h"

@implementation AppDelegate

UIBackgroundTaskIdentifier backgroundTask;
NSTimer *timer = nil;
bool processDone =false;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    backgroundTask = UIBackgroundTaskInvalid;
    timer = [NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(timerFunc) userInfo:nil repeats:YES];
    
    backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;
    }];
    NSLog(@"init : new task started (%d)", backgroundTask);
    
    dispatch_queue_t queue;
    queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        processDone = false;
        [self process];
    });
    
    return YES;
}
							

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"applicationDidEnterBackground");
}

-(void)timerFunc
{
    double remaining = [UIApplication sharedApplication].backgroundTimeRemaining;
    if (remaining < 1000.0)
        NSLog(@"Background -> time remaining = %.1f seconds", remaining);
    else
        NSLog(@"Foreground");
    
    if (processDone)
    {
        NSLog(@"processDone : endBackgroundTask (%d)", backgroundTask);
        if (backgroundTask != UIBackgroundTaskInvalid)
        {
            [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
            backgroundTask = UIBackgroundTaskInvalid;
        }

        NSLog(@"processDone : beginBackgroundTaskWithExpirationHandler");
        backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
            backgroundTask = UIBackgroundTaskInvalid;
        }];
        NSLog(@"processDone : new task started (%d)", backgroundTask);
        
        dispatch_queue_t queue;
        queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            processDone = false;
            [self process];
        });

    }
}


- (void)process
{
    sleep(5);
    processDone = true;
}

@end

解决方案

Background tasks are not wanted by Apple design. You should keep that really in mind.

A good tutorial is from the Ray Wenderlich.

Another solution can be Notifications.


这篇关于如何在iOS中管理后台连续任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆