Crashes while fetching data from filepath

2019-06-09 08:58发布

问题:

The app crashes while trying to fetch data from path/URL (video file stored in documents folder) only for large files (>100 MB).

Device Details: iOS (4.3), iPad 1

  1. URL is perfect (Checked logs)
  2. File exists at that path (checked path)

Note: Crashes on device only.

Below is the code where app crashes:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

if ([paths count] > 0) 
    {
         filePath = [paths objectAtIndex:0];
         filePath = [filePath stringByAppendingPathComponent:@"Private Documents"];
         filePath = [filePath stringByAppendingPathComponent:@"videos"];
         filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",st]];
    }

NSURL *fileUrl=[NSURL fileURLWithPath:filePath];
NSMutableData *Data = [NSMutableData dataWithContentsOfURL:fileUrl];  //CRASHES ON THIS LINE
NSLog(@"Data: %d",[Data length]);

Any comments.

回答1:

It seems you are running out of memory.

On the device there is normally far less memory available that your simulator and 100MB is a lot to be storing in RAM. Consider breaking the downloadable files into smaller chunks and deal with them in pieces if you can.

I don't think

You are taking to long time and the app gets killed.

as David suggests unless you are doing this on start up.