1

No idea what is happening here since I've followed other answers that use this including Swift: synchronously perform code in background; queue.sync does not work as I would expect

I have an XMLParser that I need to load 2 RSS feeds. To prevent reentrant parsing, I need to load these URLs one after the other is done. Problem is I need to do this in the background/async so it doesn't freeze up the UI.

I have this:

var myParser: XMLParser = XMLParser()
let parseQueue = DispatchQueue(label: "xmlQueue", attributes: [], target: nil)
var feeds = [RSS_FEED_URL_COT, RSS_FEED_URL] //this order
self.parseQueue.async {
    for f in feeds
    {
        self.loadRSSData(rssFeed: f)
    }
}

This does work meaning no reentrant error, but for a good 30 seconds whole UI is frozen up. What am I doing wrong here?

EDIT:

func loadRSSData(rssFeed: String){

        if let rssURL = URL(string: rssFeed) {

            print("LOADING THE URL: ", rssURL)

            // fetch rss content from url
            if let contents = XMLParser(contentsOf: rssURL)
            {
               self.myParser = contents 
            }

            // set parser delegate
            self.myParser.delegate = self
            self.myParser.shouldResolveExternalEntities = false

            // start parsing
            self.myParser.parse()
        }

    }

enter image description here enter image description here

blue
  • 7,175
  • 16
  • 81
  • 179
  • Without seeing `loadRSSData` no one can tell you the problem. – rmaddy May 10 '18 at 21:59
  • @rmaddy see my edit – blue May 10 '18 at 22:02
  • Seems OK. Run your code in the debugger. During the middle of the 30 seconds "freeze up", click the pause button in the debugger and see if anything is happening on the main queue. – rmaddy May 10 '18 at 22:07
  • Have you tried setting the `QOS` of your queue to `background`? Your code doesn't show it. Try updating your queue initialisation to `DispatchQueue(label: "xmlQueue", qos: .background, attributes: [], autoreleaseFrequency: .inherit, target: nil)` or `DispatchQueue.global(qos: .background)` – Malik May 11 '18 at 03:24
  • @Malik tried did not work – blue May 11 '18 at 14:32
  • @rmaddy see my edit - I believe despite putting async it is still running on main thread? Not versed in this but I put up screenshots in Q – blue May 11 '18 at 14:32
  • No, that seems correct. You are starting this from the main queue but the actual `for` loop and the parsing is being done on a background queue. – rmaddy May 11 '18 at 14:39
  • Ok.. @rmaddy still have a major freeze though. What should I do from here? This is the func holding up the UI because if I take it out it doesnt freeze – blue May 11 '18 at 14:53
  • I just created a little test iOS app. I had no trouble loading and parsing two RSS feeds in the background while the user interface remained completely useable. I used basically the same code you have in this question though I created the queue using `let parseQueue = DispatchQueue.global(qos: .background)`. If that doesn't fix your issue then it must be something in your XMLParserDelegate methods causing your issue. – rmaddy May 11 '18 at 22:50
  • @rmaddy Thanks for looking into this, I appreciate it. Hm, ok. Boy. Is there any way you'd stick your demo up on GitHub or somewhere? I'd post my delegate methods to comb through but thats too much code for an SO question, would be awesome to see a working example – blue May 11 '18 at 22:58

1 Answers1

-1

@Sjyguy,

Kindly use the loadRSSData like below.

func loadRSSData(rssFeed: String){

    if let rssURL = URL(string: rssFeed) {

        print("LOADING THE URL: ", rssURL)

        // fetch rss content from url
        if let contents = XMLParser(contentsOf: rssURL)
        {
           self.myParser = contents 
        }

        // set parser delegate
        self.myParser.delegate = self
        self.myParser.shouldResolveExternalEntities = false

        **DispatchQueue.main.async{
           // start parsing
              self.myParser.parse()
        }** 
    }
}

Hope it worked!

CrazyPro007
  • 1,006
  • 9
  • 15