I have a app with two targets: a main DeviceActivityApp target and a DeviceReport target. In the DeviceReport target, I have a TotalActivityReport struct conforming to DeviceActivityReportScene. Inside its makeConfiguration method, I update a dynamically generated list of AppReport items. The list updates correctly in the DeviceReport target.
// Define which context your scene will represent.
let context: DeviceActivityReport.Context = .totalActivity
// Define the custom configuration and the resulting view for this report.
let content: (MonitorDeviceReport) -> TotalActivityViewFirst
@ObservedObject var activityData:ActivityData
func makeConfiguration(representing data: DeviceActivityResults<DeviceActivityData>) async -> MonitorDeviceReport {
// Reformat the data into a configuration that can be used to create
// the report's view.
var appList:[AppsReport]=[]
let totalActivityDuration = await data.flatMap { $0.activitySegments }.reduce(0, {
$0 + $1.totalActivityDuration
})
for await _data in data{
for await activity in _data.activitySegments{
for await category in activity.categories{
for await app in category.applications{
let name=app.application.localizedDisplayName ?? "No Name"
let bundleId=app.application.bundleIdentifier ?? "nil"
let duration=app.totalActivityDuration
let appIcon=app.application.token
let app=AppsReport(id:bundleId,duration:duration, name:name, icon:appIcon)
appList.append(app)
}
}
}
}
DispatchQueue.main.async {
activityData.list=appList
}
return MonitorDeviceReport(duration:totalActivityDuration, apps:appList)
}
}
public class ActivityData:ObservableObject{
@Published var list:[AppsReport]=[]
public static let shared = ActivityData()
}. // This is in MonitorReport target
However, I need to access this dynamic list in my MyApp target, specifically in ContentView.swift. I tried using an ObservableObject (ActivityData) to share the data between targets, but the list always appears empty in the MyApp target.
Here’s what I’ve tried so far:
- Created a shared ActivityData instance using @Published
- Passed the ActivityData instance to
TotalActivityReport
- Used dependency injection and a singleton pattern for ActivityData
- Verified that makeConfiguration updates the list correctly in DeviceReport
What could I be missing? How can I correctly share and access this data across targets?