0

我正在创建一个 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
            });
        }
    }
}
4

0 回答 0