> On Feb 23, 2017, at 10:43 PM, Quincey Morris 
> <quinceymor...@rivergatesoftware.com> wrote:
> 
> On Feb 23, 2017, at 18:25 , Daryle Walker <dary...@mac.com 
> <mailto:dary...@mac.com>> wrote:
>> 
>>>    override func save(to url: URL, ofType typeName: String, for 
>>> saveOperation: NSSaveOperationType, completionHandler: @escaping (Error?) 
>>> -> Void) {
>>>        // Save the message data to the store so any background contexts can 
>>> read the data later.
>>>        do {
>>>            try self.container.viewContext.save()
>>>        } catch {
>>>            completionHandler(error)
>>>            return
>>>        }
>>> 
>>>        // Do the usual code, possibly even use a background thread.
>>>        super.save(to: url, ofType: typeName, for: saveOperation, 
>>> completionHandler: completionHandler)
>>>    }
>> 
>> I found out about this method from the Document guide docs. I have two 
>> questions about it.
>> 
>> 1. Is it OK to abort your override before the call to super if your 
>> pre-super code gets an error?
> 
> The catch block you’ve shown seems perfectly reasonable. Unfortunately, the 
> documentation says you can do anything you want before or after, but “be sure 
> to invoke super”, which probably makes you nervous. However, I think you can 
> assume it’s ok to return like that if an error preventing the save occurs. 
> Any other assumption seems too paranoid.
> 
> The documentation also says that the completion handler must be invoked on 
> the main thread, and I doubt you can assume that this save function is itself 
> necessarily invoked on the main thread. IAC, it’s only one extra line to 
> invoke the completion handler on the main thread, so I’d do that.

I thought the entire AppKit, which includes NSDocument, runs only on the main 
thread. The exceptions are methods that specially state that they have a 
multi-threaded mode. There are two methods on NSDocument, one each for reading 
and writing, that enable multi-threading that you have to override to return 
TRUE. If enabled, the background thread for writing is created within “save(to: 
ofType: for: completionHandler:)” and the one for reading within 
“NSDocumentController.openDocument(withContentsOf: display: 
completionHandler:)”.

— 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to