Swift-尽管XML解析器的Qos最高,但速度却非常慢?

时间:2018-09-05 16:14:22

标签: swift xml parsing rss rss-reader

好吧,我遵循了一个教程来说明这一点,所以我不知道这是什么问题-我只是在Swift中异步解析/获取来自多个RSS feed的数据。香港专业教育学院把所有的东西都放在最高的QOS(用户交互)上,但是有时,使用快速的wifi解析仍然很慢/不可靠。

我将提要存储在var feeds = [RSS_FEED_URL_COT_multiple[0], RSS_FEED_URL_COT_multiple[1], RSS_FEED_URL, RSS_FEED_WAKEUP]

然后像这样解析:

/解析--------------------------------------------- ----

// element start detected
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {


func start() {
        let q = DispatchQueue.global(qos: .userInteractive) //HERE PROBLEM
        q.async {
            for feed in self.feeds {
                if let u = URL(string: feed)
                {
                         self.parse(u)
                }
            }
        }
    }

    func parse(_ url: URL) {
        print("parsing of \(url)")
        let parser = XMLParser(contentsOf: url)!
        parser.delegate = self
        if parser.parse() {
            print("parse of \(url) complete")
        } else {
            print("parse of \(url.absoluteString) failed")

        }
    }

        if elementName == "item" {
            self.isTagFound["item"] = true
            self.rssRecord = RssRecord()

        }else if elementName == "title" {
            self.isTagFound["title"] = true

        }
        else if elementName == "content:encoded" {
            self.isTagFound["description"] = true
        }

        //Wakeup feed
        else if elementName == "description" {
            if(!self.isTagFound["description"]!)
            {
                 self.isTagFound["description"] = true
            }
        }
        else if elementName == "enclosure" {
            self.isTagFound["link"] = true
            self.isTagFound["epNum"] = true

            self.rssRecord?.link += attributeDict["url"]!
            self.rssRecord?.episodeNum += getNum(str: (self.rssRecord?.link)!)
        }
        else if elementName == "itunes:keywords" {
            self.isTagFound["keys"] = true
        }
        else if elementName == "itunes:duration" {
            self.isTagFound["duration"] = true
        }
        else if elementName == "pubDate" {
            self.isTagFound["pubDate"] = true
        }
        else if elementName == "itunes:title" {
            self.isTagFound["adLink"] = true
        }
        else if elementName == "itunes:image" {
            self.isTagFound["image"] = true

            if let str = attributeDict["href"]
            {
                //self.rssRecord?.img = setImg(urlStr: str)
                self.rssRecord?.imgStr = str
            }
        }
        else if elementName == "itunes:subtitle" {
            self.isTagFound["subtitle"] = true
        }

    }

当然,在每个URL之后,我都会进行如下排序:

func sortIntoFreeFeed()
    {
        for r in rssRecordList
        {
            if(!r.link.contains(find: wakeupSignifier))
            {
                if(r.link.contains(rssFreeSignifier) && rssFreeList.filter{$0.link == r.link}.count == 0)
                {
                    r.keys.removeAll()
                    rssFreeList.append(r)
                } else if (rssCOTList.filter{$0.link == r.link}.count == 0) {
                    rssCOTList.append(r)
                }
            }
        }

但是我不应该花很长时间来思考?这里可能是什么问题?

0 个答案:

没有答案