I think I screwed up Core Data multi-threading rules.
I think I screwed up Core Data multi-threading rules.
- Subject: I think I screwed up Core Data multi-threading rules.
- From: Daryle Walker <email@hidden>
- Date: Thu, 23 Feb 2017 01:56:35 -0500
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 (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