Post not yet marked as solved
Same issue, both Xcode 12 and 12.2 beta, reinstalling didn't help.
Post not yet marked as solved
With the new @FetchRequest property wrapper, core data fetches can occur directly in the view, per below forum discussion:https://forums.developer.apple.com/message/374407#374407SceneDelegate:import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ContentView().environment(\.managedObjectContext, context))
self.window = window
window.makeKeyAndVisible()
}
}ContentView:import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: fetchRequest(), animation: nil) var people : FetchedResults
var body: some View {
List (people, id: \.objectID) {person in
Text("\(person.lastname ?? "")")
Text("\(person.firstname ?? "")")
}
}
//Core Data Fetch Request
static func fetchRequest() -> NSFetchRequest {
let request : NSFetchRequest = Person.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "lastname", ascending: false)]
return request
}
}
Post not yet marked as solved
I got mine working by adding: ScrollView(.horizontal) {
Post not yet marked as solved
I've just been having problems trying to sort passed through data.
Post not yet marked as solved
There are some examples of using Core Data with SwiftUI showing up on github and the web:https://github.com/StephenMcMillan/Dub-Dub-Dohttps://github.com/italoboss/EmotionalDiaryhttps://medium.com/@rosscoops/swiftui-nsfetchedresultscontroller-f9f27718e3d4They seem to work with simple models (one entity), haven't seen any code based on more complex models (multi-entity with relationships) yet, nor have I been able to get a more complex model to work.