NSXMLDocument and threads
NSXMLDocument and threads
- Subject: NSXMLDocument and threads
- From: Jeff Johnson <email@hidden>
- Date: Wed, 3 Mar 2010 00:18:02 -0600
My question is, how do I use NSXMLDocument safely on a non-main thread? I need to do this for performance reasons, otherwise my app can pinwheel during XML parsing.
NSXMLDocument uses libxml. This can be verified in the debugger. Here's the thread safety documentation for libxml:
http://xmlsoft.org/threads.html
'Starting with 2.4.7, libxml2 makes provisions to ensure that concurrent threads can safely work in parallel parsing different documents. There is however a couple of things to do to ensure it:
• configure the library accordingly using the --with-threads options
• call xmlInitParser() in the "main" thread before using any of the libxml2 API (except possibly selecting a different memory allocator)'
According to a recent discussion in darwin-dev, libxml is indeed configured --with-threads:
http://lists.apple.com/archives/Darwin-dev/2010/Mar/msg00004.html
However, NSXMLDocument does not appear to call xmlInitParser() automatically. This can also be verified in the debugger.
Nonetheless, if you look at Apple's "Thread Safety Summary" doc, there's no indication that NSXMLDocument must be used on the main thread:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html
'Many objects deemed “thread-unsafe” are only unsafe to use from multiple threads. Many of these objects can be used from any thread as long as it is only one thread at a time. Objects that are specifically restricted to the main thread of an application are called out as such.'
'Main Thread Only Classes
The following class must be used only from the main thread of an application.
• NSAppleScript'
Thus, Apple's documentation gives every reason to believe that it is safe to use NSXMLDocument off the main thread, but libxml's documentation gives every reason to believe that it is not safe.
Am I supposed to manually call xmlInitParser() on the main thread at application launch? If so, am I also supposed to link my app directly to libxml? Or does Foundation.framework do some magic behind the scenes to make this all unnecessary? Am I missing something here?
-Jeff
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden