6

I'm trying to sort out an issue where a feed parsing framework (FeedKit) crashes if there is no connectivity to get the contents of the specified URL (e.g. the app is offline).

So, it works when the App is online, and online only.

Whenever I try to instantiate my Parser class with the convenience initializer of the superclass XMLParser

convenience init?(contentsOf url: URL)

The framework crashes:

enter image description here

In order to try and isolate the problem, and exclude some error introduced in the framework, I have recreated the issue in a clean project:

A solution that works like a charm, using a plain vanilla XMLParser of the Foundation framework:

let feedURL = URL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss")!

if let parser = XMLParser(contentsOf: feedURL) { // Works as expected
    print("Got instance \(parser)")
}

And another that doesn't:

class Parser: XMLParser { }

let feedURL = URL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss")!

if let parser = Parser(contentsOf: feedURL) { // Crash!
    print("Got instance \(parser)")
}

In the second example, all I'm doing is subclassing the XMLParser class. No overrides or custom code whatsoever. And it still crashes.

Am I missing something?

Thank you

Edit:

Submitted a bug report to Apple with number 28904764 and opened up a Radar to this issue.

I'm confident that this is a bug on Apple's end, but would prefer to be wrong and have a fix.

nmdias
  • 3,888
  • 5
  • 36
  • 59

2 Answers2

2

This looks like a bug.

However, you can override the designated and convenience initializer of XMLParser class in Parser and implement your own logic

class Parser: XMLParser {
    override init(data: Data)
    {
        super.init(data: data) 
    }

    convenience init?(contentsOf url: URL){
        //return data or null examining data from url
        do{
            let data = try Data(contentsOf: url)
            self.init(data: data)

        }catch{
            return nil
        }
    }
}

And call as

  let feedURL = URL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss")
  if let parser = Parser(contentsOf: feedURL!){
        print("Got instance \(parser)")
    }else{
        print("no data in url")
    }
LC 웃
  • 18,888
  • 9
  • 57
  • 72
  • Already suggested this approach as a temporary solution in the original issue https://github.com/nmdias/FeedKit/issues/4. I do not wish to provide a custom implementation of the initializer in the framework, as it defeats the purpose of the super's init. In the meanwhile, got a reply from Apple asking for the crash logs. Will update when get more feedback. – nmdias Nov 03 '16 at 11:41
  • @nmdias anyway what were you trying to know by posting a question? An alternative solution? – LC 웃 Nov 03 '16 at 11:57
  • A solution to a problem. I guess a workaround is as good as its gonna get. I'll accept your answer. – nmdias Nov 03 '16 at 12:12
-1

I think it was the problem with URL. I tried ur code I got the error.

Unable to read data

I tried replacing URL,

feedURL = NSURL(string:"http://images.apple.com/main/rss/hotnews/hotnews.rss#sthash.fuVEonEt.dpuf")!
parser = XMLParser(contentsOf:feedURL as URL)!
parser.delegate = self
parser.parse()

It work all fine. (I am using Swift 3)

Forte Zhu
  • 742
  • 3
  • 12
  • 33