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

Avoid invoking entire realm for action intents #2751

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension IntentActionAppEntity {
return nil
}

guard let result = Realm.getRealm(objectTypes: [Action.self, RLMScene.self]).object(
guard let result = Current.realm(objectTypes: [Action.self, RLMScene.self]).object(
ofType: Action.self,
forPrimaryKey: id
) else {
Expand All @@ -78,7 +78,7 @@ enum WidgetActionsDataSource {
}

static var actions: Results<Action> {
Realm.getRealm(objectTypes: [Action.self, RLMScene.self]).objects(Action.self)
Current.realm(objectTypes: [Action.self, RLMScene.self]).objects(Action.self)
.sorted(byKeyPath: #keyPath(Action.Position))
}
}
4 changes: 4 additions & 0 deletions Sources/Shared/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public class AppEnvironment {

/// Provides the Realm used for many data storage tasks.
public var realm: () -> Realm = Realm.live
/// Provides the Realm given objectTypes to reduce memory usage mostly in extensions.
public func realm(objectTypes: [ObjectBase.Type]) -> Realm {
Realm.getRealm(objectTypes: objectTypes)
}

#if os(iOS)
public var realmFatalPresentation: ((UIViewController) -> Void)?
Expand Down
4 changes: 3 additions & 1 deletion Sources/Shared/Intents/PerformActionIntentHandler.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import Intents
import PromiseKit
import Realm

class PerformActionIntentHandler: NSObject, PerformActionIntentHandling {
func handle(
Expand Down Expand Up @@ -43,7 +44,8 @@ class PerformActionIntentHandler: NSObject, PerformActionIntentHandling {
for intent: PerformActionIntent,
with completion: @escaping ([IntentAction]?, Error?) -> Void
) {
let actions = Current.realm().objects(Action.self).sorted(byKeyPath: #keyPath(Action.Position))
let actions = Current.realm(objectTypes: [Action.self]).objects(Action.self)
.sorted(byKeyPath: #keyPath(Action.Position))
let performActions = Array(actions.map { IntentAction(action: $0) })
Current.Log.info { () -> String in
"providing " + performActions.map { action -> String in
Expand Down
3 changes: 2 additions & 1 deletion Sources/Shared/Intents/WidgetActionsIntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class WidgetActionsIntentHandler: NSObject, WidgetActionsIntentHandling {
for intent: WidgetActionsIntent,
with completion: @escaping (INObjectCollection<IntentAction>?, Error?) -> Void
) {
let actions = Current.realm().objects(Action.self).sorted(byKeyPath: #keyPath(Action.Position))
let actions = Current.realm(objectTypes: [Action.self]).objects(Action.self)
.sorted(byKeyPath: #keyPath(Action.Position))
let performActions = Array(actions.map { IntentAction(action: $0) })
completion(.init(items: performActions), nil)
}
Expand Down
Loading