简体   繁体   中英

UILabel's text loads after delay

In my code I am calling this

[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    if (responseData != nil) {
        NSError *error = nil;
        NSArray *dataSource = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
        [self.ticker1 loadData:dataSource];
    } else {
        NSLog(@"Error requesting timeline %@ with user info %@.", error, error.userInfo);
    }
}];

and in the loadData: method I do this

NSDictionary *dict = [dataSource objectAtIndex:0];

_label.text = [dict objectForKey:@"text"];

dispatch_queue_t queue = dispatch_queue_create("com.test.ios.task", NULL);
dispatch_queue_t main = dispatch_get_main_queue();

dispatch_async(queue, ^{
    NSURL *imageURL = [NSURL URLWithString:[[dict objectForKey:@"user"] objectForKey:@"profile_image_url"]];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];

    dispatch_async(main, ^{
        _image.image = [UIImage imageWithData:imageData];
    });
});

dispatch_release(queue);

The problem is the image I load on main queue loads much faster than _label.text is being set. It gets set after a long delay of about 4-5 seconds. I would like to know why this is happening. Is it because the main queue is not being released or something on that lines?

I can't tell from your code which thread the request's handler block is being called on, but if it's not the main thread, that could be the problem.

Try setting the label's text property from within a

dispatch_async(main, ^{
    _label.text = [dict objectForKey:@"text"];
});

block.

The weirdest thing fixed this problem for me. I made the label hight fixed and slightly bigger then it should be, in the storyboard. It made a yellow warning in the constraints, but at least the label is loading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM