简体   繁体   中英

Save data from cancelByProducingResumeData on app close [iOS7]

I'm downloading big file ~50-100Mb with help of AFNetworking, and I want to save it's download state on app termination to resume download later. I've registered my object to terminate notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate) name:UIApplicationWillTerminateNotification object:nil];

this method called when user closing app from app switcher:

-(void)appWillTerminate {
    NSLog(@"---------- will terminate");
    [downloadTask cancelByProducingResumeData:^(NSData *resumeData) {
        if(resumeData) {
            [self saveResumeData:resumeData];
        } else {
            [[NSFileManager defaultManager] removeItemAtPath:[self createCachePath] error:nil];
        }
    }];
}

but this block: ^(NSData *resumeData) is never called so I can not save resume data.
Also this method not called at all when app was firstly moved to background and then closed. I do not want to call this method on entering background, because I want to continue downloading. Is there something I'm doing wrong?

Try to change logic: add “pause button”, if user wants to close the app he will firstly need to pause download and then close the app. In case user will ignore pausing, it's not a problem to resume downloading from position you have saved earlier or from the beginning. Main thing here - continue downloading in background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM