-
Notifications
You must be signed in to change notification settings - Fork 211
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
Use Core Data prefetching when StreamRuntimeCheck._isDatabasePrefetchingEnabled #3495
Conversation
…ingEnabled is set
extension NSFetchRequestPrefetching where Self: NSManagedObject { | ||
/// Turns off Core Data object faulting and sets prefetched relationship keypaths. | ||
/// | ||
/// Note: Reduces additional Core Data fetches when most of the data is accessed from fetched objects. | ||
static func applyPrefetchingState(to request: NSFetchRequest<Self>) { | ||
guard StreamRuntimeCheck._isDatabasePrefetchingEnabled else { return } | ||
request.returnsObjectsAsFaults = false | ||
request.relationshipKeyPathsForPrefetching = Self.prefetchedRelationshipKeyPaths() | ||
} | ||
} |
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 method is not in the NSFetchRequest
extension because extending generic Obj-C classes is complicated.
It would be nicer to write request.applyPrefetchingState()
, but did not find any good way for it which enables each subclass to define its relationship key paths. Moreover, load methods in this file make it even more complicated.
Generated by 🚫 Danger |
SDK Size
|
SDK Performance
|
SDK Size
|
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.
LGTM ✅ Lets test drive this and see if there are any improvements 👍
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.
What about the increase of hitches in the checks, do you think there might be a performance impact? Although, by default this thing is off, so weird that we get this regression, maybe something with the check itself @testableapple?
In any case, this should be tested a bit with the flag on.
SDK Performance
|
SDK Size
|
Quality Gate passedIssues Measures |
🔗 Issue Links
Part of IOS-23
🎯 Goal
📝 Summary
StreamRuntimeCheck._isDatabasePrefetchingEnabled
turns on setting:returnsObjectsAsFaults
to falserelationshipKeyPathsForPrefetching
🛠 Implementation
There have been crash reports happening when Core Data is fulfilling a fault. Instruments shows that some of the fault related requests take very long time sometimes. If the runtime check is enabled, these slow requests are not reproducible anymore. The aim of PR is to add this feature flag and see if it remedies these issues. Moreover, it makes sense to use this configuration because most of our
asModel()
functions end up accessing all the data of the Core Data model (including many relationships). For such cases, Apple also recommends setting faulting to false and using prefetching (1, 2).Tried our demo app and saw 25% reduction (4004 to 3034) of fault related requests (even when requests created by us have returnsObjectsAsFaults=false and relationship key paths set, it does remove all the fault related requests. Core Data internally makes many requests as well when merging contexts etc).
Example of the slow fault related request:
🧪 Manual Testing Notes
Set
StreamRuntimeChecks._isDatabasePrefetchingEnabled = true
and play around with the demo app. Does anything feel off?☑️ Contributor Checklist