Yes, I’ve looked into many of the already existing question, I’ve tried sample code from apple and github libraries as well as YT tutorials, and I don’t know what to do…
Basics:
I have a SwiftUI based iOS app build in Xcode with SwiftData as the main user storage. The app is fully functional and in the AppStore. Now I wanted to add a widget which (for now) should just display the title of the first object of the so called “Memory” entity.
What I’ve tried:
- Fetching the Data in the Provider with .fetch()…
- Getting access to the data though “fetching” it automatically with the @Query variable directly in the Widget view (copied from a Kavsoft tutorial – currently active)
What failed
There where different errors but currently the issue is, that the moment I add the line marked in the code (as in the tutorial) the widget, when run on a device is replaced by the placeholder. Without the Widget is displayed, the data array is empty though
struct widget: Widget {
let kind: String = "widget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
widgetEntryView(entry: entry)
.modelContainer(for: Memory.self) //this line
.containerBackground(.fill.tertiary, for: .widget)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
Yes, AppGroups are activated and I am accessing the same container from my main AppView.
I’ve also tried this, but it does not work either
.modelContainer(try! ModelContainer(for: Memory.self, configurations: ModelConfiguration("group.com.lukas.memokit.sharedata")))
The funny thing currently: In the simulator (after reinstalling the app) it works as it should, but on device (also after installing it again) it does not…
Help ????