Skip to content
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

Merged
merged 2 commits into from
Aug 7, 2023

Conversation

crazytonyli
Copy link
Contributor

@crazytonyli crazytonyli commented Aug 2, 2023

This is an attempt to fix #20630. The issue is the NSManagedObjectContext.existingObject(with:) call in AuthenticatedImageDownload 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 and Blog instances) are now used correctly, the issue would go away.

There are two main code changes:

  1. AuthenticatedImageDownload no long access NSManagedObject. It used to have code to query Blog. The code is now moved to its caller.
  2. The caller EditorMediaUtilit.downloadImage function is refactored to access the Post argument safely using performAndQuery.

Note
It might be easier to review this PR with "Hide whitespace" option on.

Test Instructions

Make sure Gutenberg editor and Aztec editor both can still load images.

Regression Notes

  1. Potential unintended areas of impact
    None.

  2. What I did to test those areas of impact (or what existing automated tests I relied on)
    What's described in the Test Instructions.

  3. What automated tests I added (or what prevented me from doing so)
    None.

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

UI Changes testing checklist: N/A

@crazytonyli crazytonyli added the Core Data Issues related to Core Data label Aug 2, 2023
@crazytonyli crazytonyli added this to the 23.0 milestone Aug 2, 2023
@crazytonyli crazytonyli requested a review from mokagio August 2, 2023 02:14
@crazytonyli crazytonyli self-assigned this Aug 2, 2023
@wpmobilebot
Copy link
Contributor

wpmobilebot commented Aug 2, 2023

Jetpack Alpha📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
App NameJetpack Alpha Jetpack Alpha
ConfigurationRelease-Alpha
Build Numberpr21227-849f018
Version22.9
Bundle IDcom.jetpack.alpha
Commit849f018
App Center Buildjetpack-installable-builds #5658
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Aug 2, 2023

WordPress Alpha📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
App NameWordPress Alpha WordPress Alpha
ConfigurationRelease-Alpha
Build Numberpr21227-849f018
Version22.9
Bundle IDorg.wordpress.alpha
Commit849f018
App Center BuildWPiOS - One-Offs #6623
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

Copy link
Contributor

@mokagio mokagio left a 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.

Comment on lines +11 to 18
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) -> ()) {
Copy link
Contributor

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
Copy link
Contributor

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 access NSManagedObject. It used to have code to query Blog. 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.

Comment on lines +160 to +180
// 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
Copy link
Contributor

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? 🤔

Copy link
Contributor Author

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.

@mokagio mokagio force-pushed the fix-core-data-issue-in-downloading-image branch from 7713175 to 849f018 Compare August 7, 2023 01:20
@peril-wordpress-mobile
Copy link

Warnings
⚠️ This PR is assigned to a milestone which is closing in less than 4 days Please, make sure to get it merged by then or assign it to a later expiring milestone

Generated by 🚫 dangerJS

@mokagio mokagio enabled auto-merge August 7, 2023 01:21
@mokagio mokagio merged commit 9215402 into trunk Aug 7, 2023
@mokagio mokagio deleted the fix-core-data-issue-in-downloading-image branch August 7, 2023 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Data Issues related to Core Data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NSInvalidArgumentException: nil is not a valid object ID
3 participants