Pergunta

This is my problem, I'm building an iOS with some JSON returs from my server, here, no problems, all works fine.

The problem is that when I run the program, it take a long long time to parse the result into a NSMutableArray: this is the log

2013-01-10 12:03:48.627 project[5917:907] <- time begin parser
2013-01-10 12:03:48.755 project[5917:907] <- time finish parser
2013-01-10 12:03:48.756 project[5917:907] <- time begin implement arrays
2013-01-10 12:03:58.522 project[5917:907] <- time finish implement array

As you can see, implementing the arrays is really long.

I know that I have to use queueing and grand central dispatch to make my UI responsive, but I don't know how to, could you please help me to do that ?

This is my viewDidLoad Method

- (void)viewDidLoad
{
    [super viewDidLoad];

    if debug
        NSLog(@"<- time begin parser");
    endif


    NSString *URLStr = @"http://myJsonFile.json";

    NSDictionary *myDictwithReturn = [TOCJSONParser awesomeParserWithStringOfYourJSONFile:URLStr]; //really cool parser, i can put it on gitHub if you want
    NSArray *speakersArray = [myDictwithReturn objectForKey:@"speakers"];

    myArray = [[NSMutableArray alloc]init];

    NSLog(@"<- time finish parser");
    NSLog(@"<- time begin implement arrays");

    for (NSDictionary *myDict in speakersArray) {

        _nextSpeaker = [[TOCSpk alloc]init];
        [_nextSpeaker setName:[myDict objectForKey:@"name"]];
        [_nextSpeaker setBusiness:[myDict objectForKey:@"business"]];
        [_nextSpeaker setDesc:[myDict objectForKey:@"desc"]];
        [_nextSpeaker setTwitter:[NSURL URLWithString:[myDict objectForKey:@"twitter"]]];
        [_nextSpeaker setPicture:[_nextSpeaker retrieveImageFromServer:[myDict objectForKey:@"picture"]]];
        [myArray addObject:_nextSpeaker];
    }

    NSLog(@"<- time finish implement array"); 
}
Foi útil?

Solução

I suspect that the slowness comes from calling retrieveImageFromServer, which lets me think that you are accessing the network. If that access is synchronous, as it seems from the fact that you are assigning the image in the same statement, than this is bound to be slow.

You should review your code and make it run on a separate thread or use asynchronous network access.

EDIT:

After your comment about using, dataWithContentsOfURL, my above hypothesis is confirmed. You can read this S.O. post about a way to download images asynchronously, or you might use any of various networking frameworks available out there.

Possibly, the easiest path forward is using SDWebImage, which is a class that offers async download for images, so you don´t have to bother yourself with thread management or NSURLConnection:

Just #import the UIImageView+WebCache.h header, and call the setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top