My app has a drafts table view that shows audio files the user saved. For the data source array, I loop through the drafts directory (inside NSDocumentDirectory
). The code works fine, and it shows all the files I've saved so far.
The problem is, just recently, all the files besides the first two are empty. By empty, I mean this: when I use NSData's datatWithContentsOfFile
method data length is 0, i.e. 0 bytes. The first two files still have data, around 267639 b each.
But if there is no data in the file, why would it appear when I loop through the drafts directory? These empty files were intact until just recently. What could have caused them to become empty?
Below is the code used to loop through the drafts directory.
// check if drafts folders exist
BOOL draftsFoldersExist = NO;
NSError *error = nil;
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *appFolderContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[folders objectAtIndex:0] error:&error];
if ([appFolderContents count] > 0 && error == nil) {
for (NSString *item in appFolderContents) {
if ([item isEqualToString:@"drafts"])
draftsFoldersExist = YES;
}
}
_draftsArray = [[NSMutableArray alloc] init];
if (draftsFoldersExist) {
// drafts data source
NSString *draftsPath = [NSString stringWithFormat:@"%@/drafts", [folders objectAtIndex:0]];
NSString *draftsPathEncoded = [draftsPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *draftsPathURL = [NSURL URLWithString:draftsPathEncoded];
// enumerate files in drafts folders
NSArray *desiredProperties = @[NSURLIsReadableKey, NSURLCreationDateKey];
NSArray *drafts = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:draftsPathURL includingPropertiesForKeys:desiredProperties options:0 error:&error];
for (NSURL *item in drafts) {
NSMutableDictionary *draftDict = [[NSMutableDictionary alloc] init];
// name
NSString *name = [item lastPathComponent];
// is readable
NSNumber *isReadableBoolValue = nil;
[item getResourceValue:&isReadableBoolValue forKey:NSURLIsReadableKey error:&error];
if ([isReadableBoolValue isEqualToNumber:@YES]) {
// filename
[draftDict setValue:[name stringByDeletingPathExtension] forKey:@"draftFilename"];
// creation date
NSDate *creationDate = nil;
[item getResourceValue:&creationDate forKey:NSURLCreationDateKey error:&error];
[draftDict setValue:creationDate forKey:@"creationDate"];
// insert into first position of data source array
_draftsArray = [[@[draftDict] arrayByAddingObjectsFromArray:_draftsArray] mutableCopy];
// meta
} else {
NSLog(@"unreadable item: %@", name);
}
}
}
UPDATE:
I downloaded the app directory's files via organizer per @Joride's suggestion and found all of the files intact and with data. Here they are:
2014-08-08_20-53-30.m4a
2014-08-09_19-11-08.m4a
2014-08-10_17-36-28.m4a
2014-08-11_18-53-46.m4a
2014-08-13_12-57-57.m4a
2014-08-16_20-44-33.m4a
2014-08-16_20-45-06.m4a
I guess the question now is why are some of them not showing any data with the dataWithContentsOfFile
method.
I use the following code to init the NSData object:
NSData *fileData = [NSData dataWithContentsOfFile:filepath options:NSDataReadingMapped error:&readingError];
For the files that have zero data, the reading error says "The operation couldn’t be completed. No such file or directory".