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

Fix widgets memory issue #2741

Merged
merged 1 commit into from
Apr 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AppIntents
import RealmSwift
import Shared
import WidgetKit

Expand All @@ -14,7 +15,10 @@ 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 +34,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 +43,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 +61,24 @@ 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
}
}

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

static var actions: Results<Action> {
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
9 changes: 8 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,12 @@ public extension Realm {

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

// swiftlint:disable cyclomatic_complexity
/// 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 {
if NSClassFromString("XCTest") != nil {
do {
return try Realm(configuration: .init(inMemoryIdentifier: "Tests", deleteRealmIfMigrationNeeded: true))
Expand Down Expand Up @@ -227,7 +233,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