LoginSignup
17
17

More than 5 years have passed since last update.

dispatch_async内でUIViewが更新されない

Posted at

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
非同期 .. 処理は止まらないがdispatch内処理後数十秒経ってからUIViewが更新される
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
同期 .. dispatch内処理中はフリーズしてしまう

UIView更新の時だけdispatch_sync dispatch_get_main_queue使う

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSError* error = nil;
                NSString* url=[NSStringstringWithFormat:@"http://"];
                NSURLRequest* request = [NSURLRequestrequestWithURL:[NSURLURLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:5.0];
                NSData* data = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror:&error];
                if (!i) {
                    dispatch_sync(dispatch_get_main_queue(), ^{ // mainキューでしかUIViewは更新されないので、更新される瞬間だけmainキューで処理
                        imagee[i].image = [UIImageimageWithData:data];
                        [self.draggableViewaddSubview:imagee[0]];
                        [self.draggableViewreloado];
                    });
                } else

                    if (i==1) {
                        dispatch_sync(dispatch_get_main_queue(), ^{
                            imagee[i].image = [UIImageimageWithData:data];
                            [self.hakooaddSubview:imagee[1]];
                        });
                    } else {
                        imagee[i].image = [UIImageimageWithData:data];
                    }           
        });

17
17
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17