0

I'm using TBXML to parse an http-XML file and display the contents in a UILabel & UIImageView . The call to the XML is done with an async request.

When i view the logs the last log element in the succesblock is printed immediately. The changes in the UILabel & UIImageview are only visible after a few seconds.

How can i let IOS refresh the UI straight after finishing processing the XML ?

// Create a success block to be called when the async request completes
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
    // If TBXML found a root node, process element and iterate all children
    NSLog(@"PROCESSING ASYNC CALLBACK");
    if (tbxmlDocument.rootXMLElement)
        [self traverseElement:tbxmlDocument.rootXMLElement];

    myArticle.Body = [[StringCleaner sharedInstance] cleanedString:myArticle.Body];
   // myArticle.Body = [myArticle.Body stringByConvertingHTMLToPlainText];
    self.articleBody.text = myArticle.Body;
    self.articleBody.numberOfLines= 0;
    self.articleBody.lineBreakMode = UILineBreakModeWordWrap;
    [self.articleBody sizeToFit];

    // set scroll view size
    self.articleBodyScrollView.contentSize = CGSizeMake(self.articleBodyScrollView.contentSize.width, self.articleBody.frame.size.height);

    NSURL *url = [NSURL URLWithString:myArticle.Photo];
    NSData *data = [NSData dataWithContentsOfURL:url];
    if (data != NULL)
    {
        UIImage *image = [UIImage imageWithData:data];
        // articlePhoto = [[UIImageView alloc] initWithImage:image];
        [self.articlePhoto setImage:image];
    }else {
        NSLog(@"no data");
    }

    NSLog(@"FINISHED PROCESSING ASYNC");


    // [self printArticles];
};

// Create a failure block that gets called if something goes wrong
TBXMLFailureBlock failureBlock = ^(TBXML *tbxmlDocument, NSError * error) {
    NSLog(@"Error! %@ %@", [error localizedDescription], [error userInfo]);
};

// tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:someXML]];
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:records] 
                           success:successBlock 
                           failure:failureBlock];
user1194465
  • 87
  • 1
  • 1
  • 7

1 Answers1

0

Sounds like your trying to update UI but not on UI thread. Wrap your UILabel and UIImageView updates into a dispatch_async on the main thread, e.g.:

dispatch_async(dispatch_get_main_queue(), ^
{
 [self.articlePhoto setImage:image];
});
CSmith
  • 13,318
  • 3
  • 39
  • 42