For anyone still having this issue - I used the following code blcok in my App.swift definition to have it work without swapping it around each time:
#if DEBUG
let dataStore = {
if (ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1") {
return DataStore.preview
} else {
return DataStore.shared
}
}()
#else
let dataStore = DataStore.shared
#endif
This little code block seems to let me work across all devices without having to swap anything around. I still can't see why this happens - it definitely isn't needed for the default template so I'm guessing there's some recursion going on somewhere in the app where the datastore is getting instantiated a second time; perhaps to do with how I'm setting up either my Commands menu or my Preferences window (as both of these take the viewContext as either a parameter or an environment respectively).
Post
Replies
Boosts
Views
Activity
I think I have managed to resolve this myself - I created a new object wrapper to resolve a different issue and it has ended up resolving this issue too.
import SwiftUI
import CoreData
struct ObjectsCommandMenu: Commands {
var context: NSManagedObjectContext
@CommandsBuilder var body: some Commands {
CommandMenu("Objects") {
NavigationLink(
destination: { NewObjectWrapper(context: context) },
label: { Text("New Object") }
)
.keyboardShortcut("o", modifiers: [.command, .shift])
Divider()
// more commands here
}
}
}
struct NewObjectWrapper: View {
var context: NSManagedObjectContext
@StateObject var object: Object
@ViewBuilder var body: some View {
ObjectEditor(object: object)
}
init(context: NSManagedObjectContext) {
self.context = context
self._object = StateObject(wrappedValue: Object(context: context))
}
}
I have this problem too - did you ever come up with a proper solution to this? Switching out the declaration in the App class gets Core Data previews working again but swapping this out every time I want to build or preview is even more annoying than just not using previews.
Also interesting to note this only happens when macOS is the preview target - setting the preview target to an iOS/iPadOS device continues to work without issue.
I have the same issue - does anyone have any pointers?
The best I have come up with is an HStack with an offset:
swift
HStack {
Text("My Text")
TextField("Enter Text", text: $myText)
}
.offset(CGSize(width: -70, height: 0))
But this is pretty ugly😬. I was considering playing around with NSFormCell - https://developer.apple.com/documentation/appkit/nsformcell but I'm not sure if this is the right way to go.