Skip to content

Commit

Permalink
Use Realm in a more memory efficient way for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Apr 25, 2024
1 parent a3e10a5 commit afd8dd5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AppIntents
import Shared
import WidgetKit
import RealmSwift

@available(iOS 17, *)
struct WidgetActionsAppIntentTimelineProvider: AppIntentTimelineProvider {
Expand All @@ -14,7 +15,7 @@ struct WidgetActionsAppIntentTimelineProvider: AppIntentTimelineProvider {
func timeline(for configuration: Intent, in context: Context) async -> Timeline<Entry> {
.init(
entries: [Self.entry(for: configuration, in: context)],
policy: .after(Current.date().addingTimeInterval(expiration.converted(to: .seconds).value))
policy: .after(Current.date().addingTimeInterval(WidgetActionsDataSource.expiration.converted(to: .seconds).value))
)
}

Expand All @@ -30,10 +31,6 @@ struct WidgetActionsAppIntentTimelineProvider: AppIntentTimelineProvider {
return WidgetActionsEntry(actions: actions)
}

private var expiration: Measurement<UnitDuration> {
.init(value: 24, unit: .hours)
}

private static func entry(for configuration: Intent, in context: Context) -> Entry {
if let existing = configuration.actions?.compactMap({ $0.asAction() }), !existing.isEmpty {
return .init(actions: existing)
Expand All @@ -43,7 +40,7 @@ struct WidgetActionsAppIntentTimelineProvider: AppIntentTimelineProvider {
}

private static func defaultActions(in context: Context) -> [Action] {
let allActions = Current.realm().objects(Action.self).sorted(byKeyPath: #keyPath(Action.Position))
let allActions = WidgetActionsDataSource.actions
let maxCount = WidgetBasicContainerView.maximumCount(family: context.family)

switch allActions.count {
Expand All @@ -61,10 +58,21 @@ extension IntentActionAppEntity {
return nil
}

guard let result = Current.realm().object(ofType: Action.self, forPrimaryKey: id) else {
guard let result = Realm.getRealm(objectTypes: [Action.self, RLMScene.self]).object(ofType: Action.self, forPrimaryKey: id) else {
return nil
}

return result
}
}

final class WidgetActionsDataSource {
static var expiration: Measurement<UnitDuration> {
.init(value: 24, unit: .hours)
}

static var actions: Results<Action> {
return Realm.getRealm(objectTypes: [Action.self, RLMScene.self]).objects(Action.self).sorted(byKeyPath: #keyPath(Action.Position))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct WidgetActionsProvider: IntentTimelineProvider {
}

private static func defaultActions(in context: Context) -> [Action] {
let allActions = Current.realm().objects(Action.self).sorted(byKeyPath: #keyPath(Action.Position))
let allActions = WidgetActionsDataSource.actions
let maxCount = WidgetBasicContainerView.maximumCount(family: context.family)

switch allActions.count {
Expand Down
2 changes: 0 additions & 2 deletions Sources/Extensions/Widgets/Assist/WidgetAssistProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ struct WidgetAssistProvider: IntentTimelineProvider {
typealias Intent = AssistInAppIntent
typealias Entry = WidgetAssistEntry

@Environment(\.diskCache) var diskCache: DiskCache

func placeholder(in context: Context) -> WidgetAssistEntry {
.init()
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/Shared/Common/Extensions/Realm+Initialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public extension Realm {

/// The live data store, located in shared storage.
static let live: () -> Realm = {
getRealm()
}

/// Mainly used to specify objectTypes in a context such as an extension, otherwise always use "Realm.live"
static func getRealm(objectTypes: [ObjectBase.Type]? = nil) -> Realm {

Check failure on line 36 in Sources/Shared/Common/Extensions/Realm+Initialization.swift

View workflow job for this annotation

GitHub Actions / lint

Function should have complexity 10 or less; currently complexity is 24 (cyclomatic_complexity)
if NSClassFromString("XCTest") != nil {
do {
return try Realm(configuration: .init(inMemoryIdentifier: "Tests", deleteRealmIfMigrationNeeded: true))
Expand Down Expand Up @@ -227,7 +232,8 @@ public extension Realm {
// Check for the realm file size to be greater than the max file size, and the amount of bytes
// currently used to be less than 50% of the total realm file size
return (realmFileSizeInBytes > maxFileSize) && (Double(usedBytes) / Double(realmFileSizeInBytes)) < 0.5
}
},
objectTypes: objectTypes
)

do {
Expand Down

0 comments on commit afd8dd5

Please sign in to comment.