美文网首页
3大后台任务

3大后台任务

作者: 我是小胡胡123 | 来源:发表于2018-05-31 16:02 被阅读9次

backgroundFetch,

是指,系统不管你APP是未启动的还是在后台的,还是挂起的。 都会以一定的时间执行performFetchWithCompletionHandler 。 可以执行30秒的任务。

与NSURLSession的backgroundSessionConfigurationWithIdentifier
是不同的东西。

backgroundSession,

NSURLSession 的backgroundSession 无需后台模式的开关 ,就支持在后台持续下载数据的。
handleEventsForBackgroundURLSession

_handleNonLaunchSpecificActions:forScene:withTransitionContext:completion:
下载完成后。唤醒app

顺序如下:
handleEventsForBackgroundURLSession-> NSURLSession的didFinishDownloadingToURL
->didCompleteWithError ->URLSessionDidFinishEventsForBackgroundURLSession


-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler {
    NSLog(@" 😊  %s %@ ",__func__, identifier);
    [LocalNotficationHelper sendLocationNotfication:@"backgroundSession~~" description:@"下载完成"];
    ///只要调用completionHandler ,立即又会进入挂起了;
     //但是挂起前,不会影响 urlSession的didfinishdownloading和didcompletWithError。但是会影响urlsessionManagerTaskDelegate中的 中的dispatch_group_async的回调。
   // completionHandler();
    [backgroundFetchManager addFinishEventsForBackgroundSesssionId:identifier block:completionHandler];
}



backgroundFetchManager.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface backgroundFetchManager : NSObject
+(void)addFinishEventsForBackgroundSesssionId:(NSString *)backgroundSessionIdentifier block: ( void (^)(void))completionBlock ;
+(void)backgroundfetchNewDataForUrl:(NSURL *)url callBackCompletion:(void (^)(UIBackgroundFetchResult result))completionBlock;

@end


backgroundFetchManager.m


#import <AFNetworking.h>
#import "backgroundFetchManager.h"
#import "LocalNotficationHelper.h"

@implementation backgroundFetchManager


+(NSMutableDictionary *)handleEventsForBackgroundURLSessionBlockDict {
    static NSMutableDictionary * handleEventsForBackgroundURLSessionBlockDict;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handleEventsForBackgroundURLSessionBlockDict  =@{}.mutableCopy;
    });
    return handleEventsForBackgroundURLSessionBlockDict;
}
+(void)addFinishEventsForBackgroundSesssionId:(NSString *)backgroundSessionIdentifier block: ( void (^)(void))completionBlock {
    
    if (backgroundSessionIdentifier  &&completionBlock ) {
        
        [[backgroundFetchManager handleEventsForBackgroundURLSessionBlockDict ] setObject:completionBlock forKey:backgroundSessionIdentifier];
    }
}



 +(void)backgroundfetchNewDataForUrl:(NSURL *)url callBackCompletion:(void (^)(UIBackgroundFetchResult result))completionBlock {
    NSString*timeString=  [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterFullStyle];
    
    NSString*file =[[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: timeString];
 
    NSURLSessionConfiguration *configuration=   [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"12345"];
     
     
     
     AFURLSessionManager*mananger= [[AFURLSessionManager  alloc] initWithSessionConfiguration:configuration];
     __weak typeof(mananger) weakMananger=  mananger;
     [mananger setDidFinishEventsForBackgroundURLSessionBlock:^(NSURLSession * _Nonnull session) {
         if (session.configuration.identifier) {
             void (^completionBlock)(void) =  [[backgroundFetchManager handleEventsForBackgroundURLSessionBlockDict] objectForKey:  session.configuration.identifier ];
             if (completionBlock) {
                 [[backgroundFetchManager handleEventsForBackgroundURLSessionBlockDict] removeObjectForKey:session.configuration.identifier];
             }
             completionBlock();
             [weakMananger invalidateSessionCancelingTasks:YES];
         }
    }];
        
    
    NSURLSessionDownloadTask*task=  [mananger  downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://down10.zol.com.cn/audio/AudioConverter_Demo.rar"]] progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@" 😊  %s : %@",__func__, @(downloadProgress.fractionCompleted));
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        NSLog(@" 😊  %s :%@",__func__, file);
        return  [NSURL fileURLWithPath:file];
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

        if (filePath&&!error) {
            NSLog(@"😊  下载成功:   %s %@",__func__, filePath);
            [LocalNotficationHelper sendLocationNotfication:@"backgroundSession~" description:[NSString stringWithFormat:@" 下载成功: 😊  %s %@",__func__, filePath]];
            completionBlock(UIBackgroundFetchResultNoData);
        } else {
            NSLog(@"😊 下载失败  %s %@",__func__, error);

            
            [LocalNotficationHelper sendLocationNotfication:@"backgroundSession~" description:[NSString stringWithFormat:@"下载失败 😊  %s %@",__func__, error]];
            completionBlock(UIBackgroundFetchResultFailed);
        }
        
       [mananger invalidateSessionCancelingTasks:YES];
    }];
    [task resume];
}
@end




remote notfication fetch

aps消息体传输增加content-available字段为1,则是以静默推送的方法是进行的。
系统不管你APP是未启动的还是在后台的,还是挂起的。
都会唤醒app
application:didReceiveRemoteNotification:fetchCompletionHandler:

{
    "aps": {
        "content-available": 1
    },
    "userinfokey1":"asdfasdfasdf"
}


相关文章

  • uap后台任务&&预警平台

    后台任务 【后台任务类型注册】 业务插件:对应的后台任务入口类nc.bs.**.**BGWorkPlugin 关键...

  • PINCache中的后台任务

    PINCache中的后台任务 在PINCache磁盘缓存策略中,针对数据写入,删除等磁盘操作,作者用了后台任务操作...

  • iOS 中的后台任务UIBackgroundTaskIdenti

    开启后台任务 This method requests additional background executi...

  • JetPack之WorkManager

    WorkManager的作用 一.确保重要的后台任务,一定会被执行,后台任务(例如:非及时性的(请求服务器及时性)...

  • 后台任务

    官方文档 Background Tasks

  • 后台任务

    https://developer.android.com/training/best-background 后台...

  • 后台任务

    http://www.ruanyifeng.com/blog/2016/02/linux-daemon.html[...

  • ios后台保活

    原理:1.开启后台任务权限,播放音乐 2.app后台后,开启后台任务,定时轮询后台剩余时间,低于20秒时候再申请新...

  • Android 后台任务

    Android 后台任务 ServiceAndroid N(Android 7.0)和 Android O(And...

  • iOS app退到后台被杀相关问题

    前言 iOS提供了后台任务的机制,利用后台任务,App可以在用户退后台后有一小段时间进行一些必要的逻辑处理(经过测...

网友评论

      本文标题:3大后台任务

      本文链接:https://www.haomeiwen.com/subject/mlwqsftx.html