• 两个与后台有关的回调处理


    1.关于程序后台数据处理

      iOS系统在程序进入后台状态后进行数据处理的时间是5s,但这个时间很短,如需在后台进行需要较长时间处理的工作,可向系统申请将这个时间延长,最长为10分钟,这段时间内程序可在后台进行相关数据操作,经测试,可以进行后台下载任务

    代码如下,在程序delegate中加入:

    #pragma mark -

    #pragma mark Background Task Handle

    - (void)applicationDidEnterBackground:(UIApplication *)application

    {

      NSLog(@"!!!%@",NSStringFromSelector(_cmd));

      UIApplication *app = [UIApplication sharedApplication];

      __block UIBackgroundTaskIdentifier taskId;

      taskId = [app beginBackgroundTaskWithExpirationHandler:^{

        NSLog(@"后台任务超时被退出");

        [app endBackgroundTask:taskId];

      }];

      if(taskId == UIBackgroundTaskInvalid)

      {

        NSLog(@"开启后台任务失败");

        return ;

      }

      dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSLog(@"后台任务据最长时限还有 %f 秒",app.backgroundTimeRemaining);

        [NSThread sleepForTimeInterval:10];

        NSLog(@"后台任务据最长时限还有 %f 秒",app.backgroundTimeRemaining);

        [app endBackgroundTask:taskId];//通知系统后台任务已处理完毕

      });

    }

    程序输出为:

    2012-01-16 11:28:06.895 backgrounddemo[46774:707] !!!applicationDidEnterBackground:

    2012-01-16 11:28:06.904 backgrounddemo[46774:1a03] 后台任务据最长时限还有 599.879219 秒

    2012-01-16 11:28:16.924 backgrounddemo[46774:1a03] 后台任务据最长时限还有 589.859110 秒

    2.后台时接收到内存不足警告的处理

    当程序收到内存警告,会隐式调用didReceiveMemoryWarning进行处理,有可能会释放掉一些资源,如背景图片、控件等,程序从后台转入执行状态后就会出现显示问题,如确定程序不是因为内存泄露而是确实为系统运行程序过多的原因(如上个函数,程序会在后台运行最多10分钟而不释放内存),可以重载didReceiveMemoryWarning函数,使其不进行处理或手动释放掉一些不使用的资源。

    代码如下,在程序delegate中加入:

    @implementation UIViewController (memoryWarning)

    - (void)didReceiveMemoryWarning

    {

      //收到内存警告时执行

      //do nothing

    }

    @end

                ---by yuzhang2

  • 相关阅读:
    uniapp--悬浮可拖动按钮-实现思路
    js获取指定日期的前一天/后一天
    适配
    使用了@Slf4j log没有info的方法 .info()方法爆红或者log爆红
    用hexo在本地搭建自己的博客
    gulp 前端构建工具使用
    springboot+idea 热部署
    配置DNS服务器转发器
    windows server2008 r2安装DNS服务器
    卸载jdk1.7
  • 原文地址:https://www.cnblogs.com/ydhliphonedev/p/2323459.html
Copyright © 2020-2023  润新知