I naively thought this was OK, multi-threading wise. It worked before I added 
the “canAsynchronusly…” method to say TRUE for my main file type. When that 
method is added, the save hangs the program.

>     override func data(ofType typeName: String) throws -> Data {
>         guard typeName == Names.internationalEmailMessageUTI else { throw 
> NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil) }
> 
>         // Save the current message data to the store so a background task 
> can see it.
>         var messageID: NSManagedObjectID?
>         var savingError: Error?
>         let mainContext = self.container.viewContext
>         mainContext.performAndWait {
>             do {
>                 try mainContext.save()
>             } catch {
>                 savingError = error
>             }
>             messageID = self.message?.objectID
>         }
> 
>         // Let the main interface continue.
>         self.unblockUserInteraction()
>         guard savingError == nil else { throw savingError! }
>         guard messageID != nil else { throw NSError(domain: 
> NSCocoaErrorDomain, code: NSCoreDataError, userInfo: nil) }
> 
>         // Write out the message data for externalization.
>         var result: Data?
>         let backgroundContext = self.container.newBackgroundContext()
>         backgroundContext.performAndWait {
>             let backgroundMessage = backgroundContext.object(with: 
> messageID!) as! RawMessage
>             result = backgroundMessage.messageAsExternalData
>         }
>         return result!
>     }

I use the original and the background copy of the message only within each of 
their respective contexts. Is the new persistent-container class not 
thread-safe even for returning (new) contexts? Am I calling 
“unblockUserInteraction” inappropriately? (I had that question in another 
post.) Is it the way I handle throwing?

— 
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