Bug 28032 - NSMutableParagraphStyle on background thread
Summary: NSMutableParagraphStyle on background thread
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: XI 8.8.0
Hardware: Macintosh Mac OS
: --- enhancement
Target Milestone: 8.13 (C6 alpha)
Assignee: Bugzilla
Depends on:
Reported: 2015-03-14 10:10 UTC by Neal
Modified: 2015-08-17 14:55 UTC (History)
7 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Neal 2015-03-14 10:10:22 UTC

It appears you're not allowing NSMutableParagraphStyle to be run on a background thread and throwing a EnsureUIThread on this usage.  Are you sure this is correct per iOS?  As far as I can tell it should not require a UI thread.

at UIKit.UIApplication.EnsureUIThread () [0x00023] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:70 
  at UIKit.NSMutableParagraphStyle.set_Alignment (UITextAlignment value) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/build/native/UIKit/NSMutableParagraphStyle.g.cs:105 
  at apdl.WhatIfGenerator.generatePDF_Page (apdl.MyContextPDF context, System.String dayPreview, Single fontSize) [0x00075] in /Users/Neal/Dev/ncsoftware/APDL/APDL.iOS/ReusableViewControllers/ReportGenerator.cs:971 
  at apdl.WhatIfGenerator+<GenerateIPhone>c__AnonStorey0.<>m__0 () [0x00032] in /Users/Neal/Dev/ncsoftware/APDL/APDL.iOS/ReusableViewControllers/ReportGenerator.cs:359 
  at ObjCRuntime.Trampolines+SDAction.Invoke (IntPtr block) [0x00019] in /Developer/MonoTouch/Source/monotouch/src/build/native/ObjCRuntime/Trampolines.g.cs:251 
  at (wrapper native-to-managed) ObjCRuntime.Trampolines/SDAction:Invoke (intptr)
  at (wrapper managed-to-native) ObjCRuntime.Messaging:void_objc_msgSendSuper_IntPtr (intptr,intptr,intptr)
  at CoreData.NSManagedObjectContext.PerformAndWait (System.Action action) [0x00054] in /Developer/MonoTouch/Source/monotouch/src/build/native/CoreData/NSManagedObjectContext.g.cs:356 
  at apdl.WhatIfGenerator.GenerateIPhone (System.String filename, apdl.Day day, apdl.Overrides overrides, System.Collections.Generic.List`1 results) [0x00041] in /Users/Neal/Dev/ncsoftware/APDL/APDL.iOS/ReusableViewControllers/ReportGenerator.cs:348 
  at apdl.LegalityDetailTableViewController.<ViewDidLoad>m__1D () [0x00069] in /Users/Neal/Dev/ncsoftware/APDL/APDL.iOS/Legality/LegalityTableViewController.cs:405 
  at apdl.PDFPageTurnViewController.<createDocumentAsync>m__0 () [0x00018] in /Users/Neal/Dev/ncsoftware/APDL/APDL.iOS/ReusableViewControllers/PDF/PDFPageTurnViewController.cs:166 
  at System.Threading.Tasks.TaskActionInvoker+ActionInvoke.Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/TaskActionInvoker.cs:71 
  at System.Threading.Tasks.Task.InnerInvoke () [0x00031] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:548 
  at System.Threading.Tasks.Task.ThreadStart () [0x000d0] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:438
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-03-16 05:43:26 UTC
(In reply to comment #0)

> As far as I can tell it should not require a UI thread.

Did you find any official documentation about it?

The Apple documentation we've found state that "For the most part, use UIKit classes only from your app’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your app’s user interface in any way." [1]

NSMutableParagraphStyle doesn't derive from UIResponder, but it's still a UIKit class, which means that we've defaulted to only allow it on the main thread (and the documentation for NSMutableParagraphStyle doesn't say anything to the contrary).

[1] https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKit_Framework/
Comment 2 Neal 2015-03-17 20:55:04 UTC

Our opinion is it should not require a UI thread such as for generating PDF reports in the background, etc. but I don't have any official documentation to share other than what you offered.  I think the assumption being more restrictive can be problematic in a case such as this, just my $.02.

Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-03-18 08:57:16 UTC
@Sebastien, what do you think about this? IMHO NSMutableParagraphStyle doesn't sound like something that should be problematic in background threads, so it'll probably be safe.
Comment 4 mobilinked 2015-05-29 23:19:00 UTC
@Rolf Bjarne Kvinge 

1. NSAttributedString is thread safe. And NSMutableParagraphStyle is the parameter of UIStringAttributes to initialise NSAttributedString. So ... 

About NSAttributeString and Thread safe, please visit 

2. Call NSMutableParagraphStyle in background thread using swift language, no problem - I think Xamarin will supports the features really work, not to support the features with official document

Please help to fix them earlier.
Comment 5 PJ 2015-08-17 14:51:33 UTC
Discussed with Sebastien, the change for this should be making C6.
Comment 6 Sebastien Pouliot 2015-08-17 14:55:41 UTC
Fixed in maccore / master 2c6a5303a71a897d216de4666ede24cf31a2b335