ios - Remove Animated UIImageView after NSOperationQueue Finishes -


i making app shows animated uiimageview custom way of indicating app busy. i'm using nsoperationqueue file uploads, , i'd uiimageview shown when there in queue. when every operation in queue completes, want remove uiimageview.

i thought easy do, i've been stuck past hour. showing uiimageview easy, can't seem remove it. it's simple i'm overlooking. here's code. thank you! :)

- (void)viewdidload {     //set uiimageview     self.spinnerview = [[uiimageview alloc] initwithframe:cgrectmake([[uiscreen mainscreen] bounds].size.width-44,0,44,44)];     self.spinnerview.animationimages = [nsarray arraywithobjects:                                      [uiimage imagenamed:@"0.gif"],                                      [uiimage imagenamed:@"1.gif"],                                      [uiimage imagenamed:@"2.gif"],                                      [uiimage imagenamed:@"3.gif"],                                      [uiimage imagenamed:@"4.gif"], nil];     self.spinnerview.animationduration = 0.5f;     self.spinnerview.tag = 998;     self.spinnerview.animationrepeatcount = 0;     [self.view addsubview: self.spinnerview];      //set queue     self.uploadqueue = [[nsoperationqueue alloc] init];     [self.uploadqueue setmaxconcurrentoperationcount:1];      //set observer queue     [self.uploadqueue addobserver:self forkeypath:@"operationcount" options:nskeyvalueobservingoptionnew context:null]; }   - (void)newupload:(nsdata*)data {     [self.spinnerview startanimating];     //....     //request nsurlrequest that's set in method     [nsurlconnection sendasynchronousrequest:request queue:self.uploadqueue completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) {     }]; }   - (void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object                      change:(nsdictionary *)change context:(void *)context {     if (object == self.uploadqueue && [keypath isequaltostring:@"operationcount"]) {         if (self.uploadqueue.operationcount == 0) {             [self.spinnerview stopanimating];         }     }     else {         [super observevalueforkeypath:keypath ofobject:object                            change:change context:context];     } 

am doing correctly? there better way it? i've been stuck here while , starting think perhaps it's not uiimageview that's messing up, rather way i'm adding nsurlrequests nsoperationqueue.

thanks again!

the documentation sendasynchronousrequest:queue:completionhandler: says operation added specified operation queue after asynchronous url request has completed. operation completion block.

so do't think adding adding operations in way intend queue. url requests running on own threads outside queue, completion blocks put on queue. if haven't specified in completion block perhaps not added queue @ all?

either way don't think adding 5 url operations queue, execute 1 after other operationcount == 5, 4, 3, 2, 1, 0. more firing 5 simultaneous url requests completion blocks being added queue in indeterminate sequence after url requests happen finish.


to think intend do, could:

  1. write "concurrent" nsoperation subclass contains , manages nsurlconnection , nsurlrequest etc.
  2. use afnetworking.
  3. continue sendasynchronousrequest:queue:completionhandler: use completion handler of 1 operation start next request, , completion handler of final request stop spinner. use main queue here work being done in queue starting next operation or stoping spinner. actual work of url request done on it's own thread anyway.

writing own concurrent nsoperation bit tricky, wrote 1 myself, should have used afnetworking. option 3 quickest if meets needs.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -