Skip to content

Commit

Permalink
Avoid invoking entire realm for action intents (#2751)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed May 2, 2024
1 parent ef1d74c commit 56d0e90
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
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

0 comments on commit 56d0e90

Please sign in to comment.