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

Move xcdatamodeld into WordPressData #24166

Closed
wants to merge 26 commits into from

Conversation

mokagio
Copy link
Contributor

@mokagio mokagio commented Mar 7, 2025

Part of #24165


Note

As I try to move more model files, I keep pulling at the intricate dependencies that grew in the codebase.
I'll be leaving comments in the diff as starting point for code to extract in order to enable this PR.

Note

Work in Progress. So far:

  • Moved the xcdatamodeld folder into the module and updated the code that referenced its URL to read it from the module
  • Run the app and saw it crash because it could not load BloggingPrompt (apparently the first model to load at runtime). The error was with Fatal error: NSArray element failed to match the Swift Array Element type . Expected BloggingPrompt got NSManagedObject.
  • Moved BloggingPrompt to WordPressData
  • App still crashed with Fatal error: NSArray element failed to match the Swift Array Element type . Expected BloggingPrompt got NSManagedObject
  • The only way I found to fix this was to hardcode the module name in the BloggingPrompt definition in the Core Data schema GUI

I don't like this solution because it would require editing the schema for each model. Besides, the compiler (or whichever piece is involved in going from the schema to the binary) should be able to inject the correct module name without hardcoding.

Leaving this up as a draft so that others can look at it.

The next thing I'll try will be to move more of the components involved in making the Core Data fetch request, the NSManagedObjectContext in particular, and see if it gets the namespace resolution to work without hardcoding.


Fixes #

To test:

Regression Notes

  1. Potential unintended areas of impact

  2. What I did to test those areas of impact (or what existing automated tests I relied on)

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

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.

Testing checklist:

  • WordPress.com sites and self-hosted Jetpack sites.
  • Portrait and landscape orientations.
  • Light and dark modes.
  • Fonts: Larger, smaller and bold text.
  • High contrast.
  • VoiceOver.
  • Languages with large words or with letters/accents not frequently used in English.
  • Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • iPhone and iPad.
  • Multi-tasking: Split view and Slide over. (iPad)

@dangermattic
Copy link
Collaborator

dangermattic commented Mar 7, 2025

3 Warnings
⚠️ Modules/Package.swift was changed without updating its corresponding Package.resolved. Please resolve the Swift packages as appropriate to your project setup (e.g. in Xcode or by running swift package resolve).
⚠️ View files have been modified, but no screenshot or video is included in the pull request. Consider adding some for clarity.
⚠️ This PR is larger than 500 lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.
1 Message
📖 This PR is still a Draft: some checks will be skipped.

Generated by 🚫 Danger

<entity name="BloggingPrompt" representedClassName=".BloggingPrompt" syncable="YES">
<entity name="BloggingPrompt" representedClassName="WordPressData.BloggingPrompt" syncable="YES">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before

image

After

image

Comment on lines +3 to +27
public let modelURL: URL = {
guard let url = Bundle.module.url(forResource: "WordPress", withExtension: "momd") else {
fatalError("Cannot find model file.")
}
return url
}()

public func urlForModel(name: String, in directory: String?) -> URL? {
let bundle = Bundle(for: TemporaryDummyClassToPickUpModule.self)
var url = bundle.url(forResource: name, withExtension: "mom", subdirectory: directory)

if url != nil {
return url
}

let momdPaths = bundle.paths(forResourcesOfType: "momd", inDirectory: directory)
momdPaths.forEach { (path) in
if url != nil {
return
}
url = bundle.url(forResource: name, withExtension: "mom", subdirectory: URL(fileURLWithPath: path).lastPathComponent)
}

return url
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These free functions should go into an object at some point.

It was just simpler to chuck them in here at this stage while I figure things out.

Also, I'd like to think there will be no need to have them public once all the types involved in setting up the Core Data stack have been moved.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Mar 7, 2025

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 Numberpr24166-43bb76a
Version25.8
Bundle IDcom.jetpack.alpha
Commit43bb76a
App Center Buildjetpack-installable-builds #10655
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 Mar 7, 2025

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 Numberpr24166-43bb76a
Version25.8
Bundle IDorg.wordpress.alpha
Commit43bb76a
App Center BuildWPiOS - One-Offs #11624
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@kean
Copy link
Contributor

kean commented Mar 7, 2025

Have you tried overriding an Objective-C class name for the managed objects written in Swift?

[@objc](BloggingPrompt)
class BloggingPrompt: NSManagedObject {

@mokagio mokagio force-pushed the mokagio/share-classNameWithoutNamespaces branch from 81890b9 to 57a9e60 Compare March 7, 2025 20:56
Base automatically changed from mokagio/share-classNameWithoutNamespaces to trunk March 7, 2025 21:51
@mokagio
Copy link
Contributor Author

mokagio commented Mar 10, 2025

Have you tried overriding an Objective-C class name for the managed objects written in Swift?

[@objc](BloggingPrompt)
class BloggingPrompt: NSManagedObject {

@kean yes I did try that. I removed it from the code pushed here because it did not make any difference

@mokagio mokagio force-pushed the mokagio/move-xcdatamodeld-to-wordpressdata branch from b94d69d to e1eb0b3 Compare March 11, 2025 00:42
mokagio added 12 commits March 11, 2025 15:58
This is so the diff is neater next when moving it to WordPressShared
Turned out there needed to be move access control changes than
those done in the previous commit...
We'll obviously have to deal with this at some point, but doing it while
moving the files from the app targets to the new module is not the right
time.
Swift-ier + Saves making `sharedInstance()` public when migrating
Not the best approach, but fine because the property is for testing
only.
Currently unused, but we'll use them soon when moving more of the
Objective-C models.
@mokagio mokagio force-pushed the mokagio/move-xcdatamodeld-to-wordpressdata branch from 58f69c3 to b0517a3 Compare March 11, 2025 10:07
mokagio added 8 commits March 12, 2025 14:53
All those Swift files compile without importing UIKit because
`SFHFKeychainUtils.h` did that for them and they got access to it via
the bridging header.

Next, we'll move SFHFKeychainUtils into the modules, and UIKit will no
longer be available through it.

Side note: UIKit should not have been available via SFHFKeychainUtils in
the first place. On top of that, SFHFKeychainUtils does not need UIKit
either.
We need it in a dedicated package because the Objective-C implementation
does not use ARC. Therefore, we need to use the `-fno-objc-arc` compiler
setting. But such a setting should be applied sparingly so instead of
blanket using it in an existing Objective-C package like
WordPressSharedObjC, we use it surgically just for this file.
This is to decouple the Objective-C implementation from knowing about
components (i.e. `ContextManager`) in the Swift layer.
mokagio added a commit that referenced this pull request Mar 12, 2025
To much Objective-C code using logic implemented elsewhere in Swift for
us to be able to move the files in two separate Objective-C- and Swift-
only packages.

Refer to #24166 as
an experiment for this that shows the various build failure that even a
partial attempt at moving some files results in.
mokagio added a commit that referenced this pull request Mar 13, 2025
To much Objective-C code using logic implemented elsewhere in Swift for
us to be able to move the files in two separate Objective-C- and Swift-
only packages.

Refer to #24166 as
an experiment for this that shows the various build failure that even a
partial attempt at moving some files results in.
mokagio added a commit that referenced this pull request Mar 14, 2025
To much Objective-C code using logic implemented elsewhere in Swift for
us to be able to move the files in two separate Objective-C- and Swift-
only packages.

Refer to #24166 as
an experiment for this that shows the various build failure that even a
partial attempt at moving some files results in.
mokagio added a commit that referenced this pull request Mar 17, 2025
To much Objective-C code using logic implemented elsewhere in Swift for
us to be able to move the files in two separate Objective-C- and Swift-
only packages.

Refer to #24166 as
an experiment for this that shows the various build failure that even a
partial attempt at moving some files results in.
mokagio added a commit that referenced this pull request Mar 18, 2025
To much Objective-C code using logic implemented elsewhere in Swift for
us to be able to move the files in two separate Objective-C- and Swift-
only packages.

Refer to #24166 as
an experiment for this that shows the various build failure that even a
partial attempt at moving some files results in.
@mokagio
Copy link
Contributor Author

mokagio commented Mar 19, 2025

Closing in favor of #24242

@mokagio mokagio closed this Mar 19, 2025
@mokagio mokagio deleted the mokagio/move-xcdatamodeld-to-wordpressdata branch March 19, 2025 00:12
mokagio added a commit that referenced this pull request Mar 19, 2025
To much Objective-C code using logic implemented elsewhere in Swift for
us to be able to move the files in two separate Objective-C- and Swift-
only packages.

Refer to #24166 as
an experiment for this that shows the various build failure that even a
partial attempt at moving some files results in.
mokagio added a commit that referenced this pull request Mar 20, 2025
To much Objective-C code using logic implemented elsewhere in Swift for
us to be able to move the files in two separate Objective-C- and Swift-
only packages.

Refer to #24166 as
an experiment for this that shows the various build failure that even a
partial attempt at moving some files results in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants