WidgetKit Core Data

Hello,

I created a widget kit extension for my app and would like it to pull information from the iOS coredata model. I can not figure this out. Here is my code for the widget. Thanks in advance.

Code Block
2020-06-23 19:15:50.041978-0400 AssignmentsExtension[94980:3425649] [error] error: +[Assignments entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[Assignments entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
Program ended with exit code: 9


Code Block
struct LeaderboardEntry: TimelineEntry {
    public let date: Date
    public let assign: String?
}
 struct Provider: TimelineProvider {
    @FetchRequest(entity: Assignments.entity(), sortDescriptors: []) var contactsreq: FetchedResults<Assignments>
    @State var s: String?
   
    public func snapshot(with context: Context,completion: @escaping (LeaderboardEntry) -> ()) {
        let entry = LeaderboardEntry(date: Date(), assign: contactsreq[0].name!)
        s = contactsreq[1].name
        completion(entry)
    }
    public func timeline(with context: Context,
                            completion: @escaping (Timeline<Entry>) -> ()) {
        let entry = LeaderboardEntry(date: Date(), assign: contactsreq[1].name))
           let timeline = Timeline(entries: [entry, entry], policy: .atEnd)
           completion(timeline)
       }
}
struct AssignmentsEntryView : View {
    func dateToSting(d: Date) -> String{
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EE MMMM d, YYYY"
               
        return dateFormatter.string(from: d)
    }
    
   var entry: LeaderboardEntry
 
    var body: some View {
   
        Text(entry.assign!)
            
        
    
                      
                   }
            
        }
@main
struct AssignmenstWidget: Widget {
    var body: some WidgetConfiguration {
        
        StaticConfiguration(
            kind: "com.mygame.game-status",
            provider: Provider(),
            placeholder: PlaceholderView()
        ) { entry in
            AssignmentsEntryView(entry: entry)
        }
        .configurationDisplayName("Game Status")
        .description("Shows an overview of your game status")
        .supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
    }}


 

 




Answered by Systems Engineer in 614411022
Because WidgetKit extensions are a separate process from your main application, sometimes sharing data between them can be a bit tricky.

The error here is

Failed to find a unique match for an NSEntityDescription to a managed object subclass

(Bolding/Italics is mine)

This means that when CoreData went to lookup your entity it needed to find a class that matched. It said it couldn't which means one of a few things:
  1. Your data model classes could not be accessed by the WidgetKit extension. You may need to put them in a framework and import that framework to your WidgetKit extension.

  2. Your data model file was not found by your WidgetKit extension. You may need to include this xcdatamodeld file with your WidgetKit extension by clicking the checkbox in the File Inspector in Xcode. (Click the data model file, open the right sidebar in Xcode, go to the first tab, find the name of your WidgetKit extension, and click the checkbox)

  3. Something is misconfigured with your app groups. See the App Extension Programming Guide which has helpful steps.

Accepted Answer
Because WidgetKit extensions are a separate process from your main application, sometimes sharing data between them can be a bit tricky.

The error here is

Failed to find a unique match for an NSEntityDescription to a managed object subclass

(Bolding/Italics is mine)

This means that when CoreData went to lookup your entity it needed to find a class that matched. It said it couldn't which means one of a few things:
  1. Your data model classes could not be accessed by the WidgetKit extension. You may need to put them in a framework and import that framework to your WidgetKit extension.

  2. Your data model file was not found by your WidgetKit extension. You may need to include this xcdatamodeld file with your WidgetKit extension by clicking the checkbox in the File Inspector in Xcode. (Click the data model file, open the right sidebar in Xcode, go to the first tab, find the name of your WidgetKit extension, and click the checkbox)

  3. Something is misconfigured with your app groups. See the App Extension Programming Guide which has helpful steps.

WidgetKit Core Data
 
 
Q