Revelation with GCD and NSXMLParser
Recently while I was doing some work on an RSS parser for an iPhone application, I was having an issue with the parser not showing all the results, so I went and logged a bunch of stuff trying to figure out if it was just not returning enough stuff or not, and it turned out that it was, so that wasn't the problem, and every time I ran the code it would return a different amount of items, so I was looking through the code and decided to try something, which actually found me the correct answer, and that was what the difference between dispatch_async()
and dispatch_sync()
. So I'll go quickly through the difference.
Now, the allocation and other stuff isn't any different than usual
and right where the dispatch_async() is where the problem begins, as dispatch_async() returns immediately any results parsed in the fractions of seconds between the [rssParser parse];
and the tableview:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
are placed into your parsed results array, and any others after that are just orphaned into the bit bucket by not returning quickly enough. the fix to this is actually just one letter: