-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove the Core Data workaround in EditorMediaUtility #21227
Conversation
📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
|
📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure Gutenberg editor and Aztec editor both can still load images.
I verified this via the Jetpack installable build.
init(with blog: Blog) { | ||
self.init(with: blog) { error in | ||
// We'll log the error, so we know it's there, but we won't halt execution. | ||
WordPressAppDelegate.crashLogging?.logError(error) | ||
} | ||
} | ||
|
||
init(with blog: Blog, failure: (BlogError) -> ()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered using argument default for this over a dedicated init?
E.g.
init(
with blog: Blog,
failure: (BlogError) -> () = { error in
// ...
}
) {
Granted this approach assumes that the logging mechanism will remain a singleton shared instance, which might not be desirable in the future.
let blogObjectID: NSManagedObjectID | ||
let mediaHost: MediaHost |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the PR description:
AuthenticatedImageDownload
no long accessNSManagedObject
. It used to have code to queryBlog
. The code is now moved to its caller.
Just wanted to +1 this decision. Aside from the hunch that this will help with #20630, having the image download operation know about MediaHost
over Blog
seems like a better design because the surface of information it can access (thus that we need to be aware of when working with it) is now much smaller.
// This function is added to debug the issue linked below. | ||
let safeExistingObject: (NSManagedObjectID) throws -> NSManagedObject = { objectID in | ||
var object: Result<NSManagedObject, Error> = .failure(AuthenticatedImageDownload.DownloadError.blogNotFound) | ||
do { | ||
// Catch an Objective-C `NSInvalidArgumentException` exception from `existingObject(with:)`. | ||
// See https://github.com/wordpress-mobile/WordPress-iOS/issues/20630 | ||
try WPException.objcTry { | ||
object = Result { | ||
try context.existingObject(with: objectID) | ||
} | ||
} | ||
} catch { | ||
// Send Objective-C exceptions to Sentry for further diagnosis. | ||
WordPressAppDelegate.crashLogging?.logError(error) | ||
throw error | ||
} | ||
|
||
return try object.get() | ||
} | ||
|
||
let post = try safeExistingObject(postObjectID) as! Post |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is neat.
Hopefully we won't have to use it again, but just in case, I wonder how we could make it more discoverable / flexible so other people can reuse it? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really hope that this PR fixes the issue and makes the extra Objective-C exception become unnecessary. That means we can delete this ugly try catch.
I don't want to move this into a shared places because I don't think we should catch the NSInvalidArgument
exception, which to me feels like an indication of programming error, rather than Core Data data accessing error.
7713175
to
849f018
Compare
Generated by 🚫 dangerJS |
This is an attempt to fix #20630. The issue is the
NSManagedObjectContext.existingObject(with:)
call inAuthenticatedImageDownload
throws an Objective-C exception.The root cause is unknown. But this PR cleans up the code that accesses Core Data objects. Hopefully now that the Core Data objects (
Post
andBlog
instances) are now used correctly, the issue would go away.There are two main code changes:
AuthenticatedImageDownload
no long accessNSManagedObject
. It used to have code to queryBlog
. The code is now moved to its caller.EditorMediaUtilit.downloadImage
function is refactored to access thePost
argument safely usingperformAndQuery
.Test Instructions
Make sure Gutenberg editor and Aztec editor both can still load images.
Regression Notes
Potential unintended areas of impact
None.
What I did to test those areas of impact (or what existing automated tests I relied on)
What's described in the Test Instructions.
What automated tests I added (or what prevented me from doing so)
None.
PR submission checklist:
RELEASE-NOTES.txt
if necessary.UI Changes testing checklist: N/A