ios - Empty files in app documents directory -
my app has drafts table view shows audio files user saved. data source array, loop through drafts directory (inside nsdocumentdirectory
). code works fine, , shows files i've saved far.
the problem is, recently, files besides first 2 empty. empty, mean this: when use nsdata's datatwithcontentsoffile
method data length 0, i.e. 0 bytes. first 2 files still have data, around 267639 b each.
but if there no data in file, why appear when loop through drafts directory? these empty files intact until recently. have caused them become empty?
below code used loop through 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) { (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]; (nsurl *item in drafts) { nsmutabledictionary *draftdict = [[nsmutabledictionary alloc] init]; // name nsstring *name = [item lastpathcomponent]; // 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 first position of data source array _draftsarray = [[@[draftdict] arraybyaddingobjectsfromarray:_draftsarray] mutablecopy]; // meta } else { nslog(@"unreadable item: %@", name); } } }
update:
i downloaded app directory's files via organizer per @joride's suggestion , found of files intact , data. here 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 question why of them not showing data datawithcontentsoffile
method.
i use following code init nsdata object:
nsdata *filedata = [nsdata datawithcontentsoffile:filepath options:nsdatareadingmapped error:&readingerror];
for files have 0 data, reading error says "the operation couldn’t completed. no such file or directory".
i figured out problem. app's document directory changed @ point, location of files changed.
files saved here:
/var/mobile/applications/09e7c349-6d03-4d2f-be17-46c00b17c9f5/documents/drafts/2014-08-13_12-57-57.m4a
and later here:
/var/mobile/applications/1a444a31-c29d-4b0f-8b47-a5b57d7f3281/documents/drafts/2014-08-16_20-45-06.m4a
notice app's folder has different token. files "there" not there guess. apparently app's directory can change when new provisioning profile used or there's update app store (see this answer).
so moral of story is: don't save absolute paths reference file.
Comments
Post a Comment