Re: I think I screwed up Core Data multi-threading rules.
Re: I think I screwed up Core Data multi-threading rules.
- Subject: Re: I think I screwed up Core Data multi-threading rules.
- From: Daryle Walker <email@hidden>
- Date: Sat, 25 Feb 2017 17:58:23 -0500
> On Feb 23, 2017, at 10:43 PM, Quincey Morris <email@hidden> wrote:
>
> On Feb 23, 2017, at 18:25 , Daryle Walker <email@hidden <mailto:email@hidden>> 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 (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