Post

Replies

Boosts

Views

Activity

Reply to WidgetKit and CoreData/CloudKit
Hi cristosv, Thanks for posting your solution to this online. I'm having the same problem, but it would be great it for you could help a little more. For example, all your code is working fine, but I can't seem to figure out how to use my core data entity in a Text() view in my widget. Below is my code, and any help with this or more sample code would be great. Thanks 🙏 import WidgetKit import SwiftUI import Intents import CoreData struct Provider: IntentTimelineProvider {     func placeholder(in context: Context) -> SimpleEntry {         SimpleEntry(date: Date(), configuration: ConfigurationIntent())     }     func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {         let entry = SimpleEntry(date: Date(), configuration: configuration)         completion(entry)     }     func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {         var entries: [SimpleEntry] = []         // Generate a timeline consisting of five entries an hour apart, starting from the current date.         let currentDate = Date()         for hourOffset in 0 ..< 5 {             let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!             let entry = SimpleEntry(date: entryDate, configuration: configuration)             entries.append(entry)         }         let timeline = Timeline(entries: entries, policy: .atEnd)         completion(timeline)     }     var managedObjectContext : NSManagedObjectContext     init(context : NSManagedObjectContext) {         self.managedObjectContext = context     } } struct SimpleEntry: TimelineEntry {     let date: Date     let configuration: ConfigurationIntent } struct Test_WidgetEntryView : View {     var entry: Provider.Entry     @FetchRequest(entity: Order.entity(),                       sortDescriptors: [],                       predicate: NSPredicate(format: "status != %@", Status.completed.rawValue))                  var orders: FetchedResults<Order>     var body: some View {         Text("This is where I would like to display the text from one of the entity elements")     } } @main struct Test_Widget: Widget {     let kind: String = "Test_Widget"     public var body: some WidgetConfiguration {         IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider(context: persistentContainer.viewContext)) { entry in             Test_WidgetEntryView(entry: entry)         }             .configurationDisplayName("My Widget")             .description("This is an example widget.")         } //     // MARK: - Core Data stack     var persistentContainer: NSPersistentCloudKitContainer = {         /*          The persistent container for the application. This implementation          creates and returns a container, having loaded the store for the          application to it. This property is optional since there are legitimate          error conditions that could cause the creation of the store to fail.         */         let container = NSPersistentCloudKitContainer(name: "PostMaster")     //    let storeURL = URL.storeURL(for: "group.com.seannagle.ipostmaster", databaseName: "iPostMaster")     //    let storeDescription = NSPersistentStoreDescription(url: storeURL)         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 /*                  Typical reasons for an error here include:                  * The parent directory does not exist, cannot be created, or disallows writing.                  * The persistent store is not accessible, due to permissions or data protection when the device is locked.                  * The device is out of space.                  * The store could not be migrated to the current model version.                  Check the error message to determine what the actual problem was.                  */                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })     //    container.persistentStoreDescriptions = [storeDescription]         container.viewContext.automaticallyMergesChangesFromParent = true         container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy         return container     }() }
Aug ’20