scinfu / SwiftSoup

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
https://scinfu.github.io/SwiftSoup/
MIT License
4.52k stars 345 forks source link

XML parse blocks main thread #233

Open tomasbek opened 1 year ago

tomasbek commented 1 year ago

Dear all,

I use SwiftSoup to extract XML data for my SwiftUI app. It works perfectly but recently I run up to a problem when working with very large XML files (10000 lines). Even though I use SwiftUI's async and task functionality to parse the data in the background, it still blocks the main thread.

Please see the sample code below to explain the situation.

@State var data: [Element] = []

List { 
     ForEach(data) { element in
       ElementRow(element: element)
     }
}
.task {
     let (data, _) = try await session.data(from: some url)
     let xml = String(data: data, encoding: .utf8)
     let document = try SwiftSoup.parse(xml, "", Parser.xmlParser()) // <- This is where the hang comes up from
     <...>
     self.data = result
}

Any ideas on way forward to avoid the app hanging up while SwiftSoup parses large XML file?

Thanks!

rohit9934 commented 1 year ago

I suspect this is concurrency problem rather than SwiftSoup issue, can you try running the operation on different queues?

aehlke commented 7 months ago

this isn't a library issue