I am trying to display my core data values in the widget kit however my values are not displayed. I am sharing my code below:
I was following different tutorials for the same task and thus had followed different ways to achieve the task. I tried app groups as well but couldn't succeed.
Result: The widget UI has been done but not even the static texts are displayed when I write the shared code.
My main purpose is to display the user details stored in the core data into my widget.
Here is my code base :
struct Provider: AppIntentTimelineProvider {
let managedObjectContext: NSManagedObjectContext
let fetchRequest: NSFetchRequest<User> = User.fetchRequest()
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), valueToDisplay: "dfbgdfb")
}
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> SimpleEntry {
SimpleEntry(date: Date(), valueToDisplay: "4534534")
}
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<SimpleEntry> {
var entries: [SimpleEntry] = []
let users = try? managedObjectContext.fetch(fetchRequest)
// Process fetched data and create SimpleEntry objects
for user in users! {
let entry = SimpleEntry(date: Date(), valueToDisplay: user.userName ?? "")
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
return timeline
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let valueToDisplay : String
}
struct QuitSmokeAllWidgetEntryView : View {
var entry: Provider.Entry
@Environment(\.managedObjectContext) private var viewContext
var body: some View {
//MARK: - QuitSmoke Widgets
ZStack {
Color("ThemeColor")
.ignoresSafeArea(.all)
VStack {
Spacer()
Text("Money Saved")
Text(entry.valueToDisplay) // Display the core data value
.foregroundColor(.white)
Text("$0,0")
.foregroundColor(.white)
.padding()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color("ThemeColor"))
}
.edgesIgnoringSafeArea(.all)
}
}
struct QuitSmokeAllWidget: Widget {
let kind: String = "QuitSmokeAllWidget"
var body: some WidgetConfiguration {
AppIntentConfiguration(kind: kind, intent: ConfigurationAppIntent.self, provider: Provider(managedObjectContext: NSManagedObjectContext())) { entry in
QuitSmokeAllWidgetEntryView(entry: entry)
.containerBackground(Color("ThemeColor"), for: .widget)
}
}
}
Here is my code for Persistence.swift file:
public extension URL {
static func storeUrl(for appGroup:String, databaseName:String) -> URL {
guard let fileConatiner = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {
print("Unable to create url for \(appGroup)")
return URL(fileURLWithPath: "")
}
return fileConatiner.appendingPathComponent("\(databaseName).sqlite")
}
}
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "Quit_Smoking")
if inMemory {
container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
} else {
let url = URL.storeUrl(for: "group.com.quitSmoke", databaseName: "Quit_Smoking")
let storeDescription = NSPersistentStoreDescription(url: url)
container.persistentStoreDescriptions = [storeDescription]
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
print("ERRRORRR--->\(error.localizedDescription)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
}
Any help will be appreciated.
Thanks in advance.