ios - Saving and reading objects from plist cause huge memory of 2G -
i have system save lots of images plist , works good, except 2 main problems.
first, when start process ,memory on xcode goes 2g ! (and down when done) second, takes long(10+ seconds 100 images)compared nsuserdefaults
, told slower .
i archiving data first.
what doing wrong having memory , slow saving ?
-(void)savetofilewithdata:(nsmutabledictionary*)dic { nserror *error; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"data.plist"]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; if (![filemanager fileexistsatpath: path]) { nsstring *bundle = [[nsbundle mainbundle] pathforresource:@"data" oftype:@"plist"]; [filemanager copyitematpath:bundle topath: path error:&error]; } nsdata *mydata = [nskeyedarchiver archiveddatawithrootobject:dic]; bool sucess=[mydata writetofile:path atomically:yes]; if(sucess) nslog(@"saved:%lu",(unsigned long)[mydata length]); else nslog(@"failed:%lu",[mydata length]); mydata=nil; }
the reading :
-(nsmutabledictionary*)readfromfile { nserror *error; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); //1 nsstring *documentsdirectory = [paths objectatindex:0]; //2 nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"data.plist"]; //3 nsfilemanager *filemanager = [nsfilemanager defaultmanager]; if (![filemanager fileexistsatpath: path]) //4 { nsstring *bundle = [[nsbundle mainbundle] pathforresource:@"data" oftype:@"plist"]; //5 [filemanager copyitematpath:bundle topath: path error:&error]; //6 } nsmutabledictionary *dic = [[ nsmutabledictionary alloc] init]; nsdata *serialized = [nsdata datawithcontentsoffile:path]; //check first if file exist, if has content(empty file had 42 bytes-and crashes archiver) if ([[nsfilemanager defaultmanager] fileexistsatpath:path] && [serialized length]>1000000) dic = (nsmutabledictionary*) [nskeyedunarchiver unarchiveobjectwithdata:serialized]; serialized=nil; return dic; }
use them :
nsmutabledictionary *dic = [[nsmutabledictionary alloc] init]; dic = [self readfromfile]; //change dic [self savetofilewithdata:dic];
there no reason copy plist app bundle documents directory read it.
a plist not solution, in case huge put images individual files , image file names in plist. read small plist , rest images 1 one. reading each file individually uses smaller amount of memory.
but, want 500mb of images in memory @ once? after having read plist of image information read images needed, perhaps cache purges images based on usage.
Comments
Post a Comment