iOS后台下载失败

时间:2013-01-12 18:16:26

标签: ios

我正在创建一个iOS应用程序,我需要下载超过100个视频,每个视频大约5-10 MB。有5天,每天都有多个视频。我将视频和URL列表存储在数据库中。问题是我无法使用AFNetworking,因此我尝试通过创建后台流程使用NSData下载视频。

这一直运行良好,但经过几次,它暂停,我必须重新启动应用程序才能再次开始下载。我不明白到底出了什么问题。

以下是我的代码:

-(void)downloadVideos{

    Database * database = [[Database alloc]init];

    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) {
        //Check if our iOS version supports multitasking I.E iOS 4
        if ([[UIDevice currentDevice] isMultitaskingSupported]) {
            //Check if device supports mulitasking
            UIApplication *application = [UIApplication sharedApplication];
            //Get the shared application instance
            __block UIBackgroundTaskIdentifier background_task;
            //Create a task object
            background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
                [application endBackgroundTask: background_task];
                //Tell the system that we are done with the tasks
                background_task = UIBackgroundTaskInvalid;
                //Set the task to be invalid
                //System will be shutting down the app at any point in time now
            }];
            //Background tasks require you to use asyncrous tasks
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                //Perform your tasks that your application requires
                NSLog(@"\n\nRunning in the background!\n\n");

                for (int j = 1; j <= 5; j++) {

                    NSArray * filesGYM = [database getVideoURLsFromDBToDownloadVideos:j vType:@"gym" advWeekNum:1];
                    NSArray * filesGYMIDs = [database getVideoIDsFromDBToDownloadVideos:j vType:@"gym" advWeekNum:1];

                    for (int i = 0; i < [filesGYM count]; i++) {
                        NSString * tempFile = [filesGYM objectAtIndex:i];
                        NSString *file = [tempFile stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

                         NSData * theVideo = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:file]];

                         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                         NSString *documentsDirectory = [paths objectAtIndex:0];
                         if ([theVideo writeToFile:[documentsDirectory   stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.mp4",[filesGYMIDs objectAtIndex:i] ]] atomically:YES]) {
                         NSLog(@"File No.- %@ Downloaded",[filesGYMIDs objectAtIndex:i]);
                         [database setVideoDownloaded:tempFile];

                         } else {
                         NSLog(@"File  No.- %d - %@ - Downloading Failed",i+1,[filesGYMIDs objectAtIndex:i]);

                         }
                    }
                }

                [application endBackgroundTask: background_task];
                //End the task so the system knows that you are done with what you need to perform
                background_task = UIBackgroundTaskInvalid;
                //Invalidate the background_task
            });
        }
    }
}

0 个答案:

没有答案