we developing enterprise app our company.the app supposed work offline, means need download more 10k product images ipad.each image can several kb 4m.for doing @ first used following code download images:
-(bool)downloadfileinbatch :(nsarray*) filearr { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentdirectory = [paths objectatindex:0]; nsoperationqueue *queue = [[nsoperationqueue alloc] init]; queue.maxconcurrentoperationcount = 4; nsblockoperation *completionoperation = [nsblockoperation blockoperationwithblock:^{ [[nsoperationqueue mainqueue] addoperationwithblock:^{ [[nsnotificationcenter defaultcenter] postnotificationname:@"taskdone" object:self]; }]; }]; @autoreleasepool { nsblockoperation *operation; for(int = 0; < filearr.count; i++) { operation = [nsblockoperation blockoperationwithblock:^{ nsurl *url = [[nsurl alloc] initwithstring:[[filearr objectatindex:i] objectforkey:@"image_url"]]; nsdata *data = [nsdata datawithcontentsofurl:url]; nsstring *filename = [documentdirectory stringbyappendingstring:[nsstring stringwithformat:@"/%@", [url lastpathcomponent]]]; nslog(@"downloading file : %@ : no : %d", [documentdirectory stringbyappendingstring:[nsstring stringwithformat:@"/%@", [url lastpathcomponent]]], i); bool success = [data writetofile:filename atomically:yes]; if(!success) { // failed download } }]; [completionoperation adddependency:operation]; } } [queue addoperations:completionoperation.dependencies waituntilfinished:no]; [queue addoperation:completionoperation]; return yes; }
but when using code download, app crash due memory problem.(even putting @autoreleasepool did not solve crashing problem). seems downloading one-by-one (or 4 images parallel)is not suitable option amount of data since cause memory problem.i want know best way download amount of images server ipad?
thanks in advance...
Comments
Post a Comment