ios - NSURL download file, indicators shows too late -
hi got following code , want show activityindicator while downloading. indicator shows when download has finished?
_fandownloadindicator.hidden = no; nslog(@"batzn"); nsstring *documentpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)objectatindex:0]; nsstring *file = [documentpath stringbyappendingpathcomponent:@"fan_new.mp3"]; bool fileexists = [[nsfilemanager defaultmanager]fileexistsatpath:file]; if(fileexists == no) { nsurl *downurl = [nsurl urlwithstring:url]; nsdata *data = [nsdata datawithcontentsofurl:downurl]; if ([data writetofile:file atomically:yes]) { nslog(@"downloaded fan");
issue
you doing download task on main thread, , wait until thread ends before show loading view.
solution
you need start downloading task on background thread using dispatch_async. check out code below
_fandownloadindicator.hidden = no; nslog(@"batzn"); nsstring *documentpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)objectatindex:0]; nsstring *file = [documentpath stringbyappendingpathcomponent:@"fan_new.mp3"]; bool fileexists = [[nsfilemanager defaultmanager]fileexistsatpath:file]; if(fileexists == no) { //show loading view here dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_high, 0), ^{ //do downloading here on background thread nsurl *downurl = [nsurl urlwithstring:url]; nsdata *data = [nsdata datawithcontentsofurl:downurl]; dispatch_async(dispatch_get_main_queue(), ^{ if ([data writetofile:file atomically:yes]) { //hide loading view nslog(@"downloaded fan"); } }); }); }
also i'll provide suggestion loading view mbprogresshud. here can it
//show loading form here [mbprogresshud showhudaddedto:self.view animated:yes]; dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_low, 0), ^{ // something... dispatch_async(dispatch_get_main_queue(), ^{ [mbprogresshud hidehudforview:self.view animated:yes]; }); });
update
if dealing downloading images, should recommend library:
it offers third party show activity indicator.
Comments
Post a Comment