Yes to open a new window in a SwiftUI macOS app use @Environment openURL, WindowGroup's handlesExternalEvents and Project->Info->URL Types.
Below is a copy of a tutorial I posted on my blog (I wasn't allowed to include a link to it).
Here is how to open a new window in SwiftUI on macOS.
In your ContentView create a button and open a URL for your app and another View e.g. Viewer to be shown in the window we will open:
struct ContentView: View {
		@Environment(\.openURL) var openURL
		var body: some View {
				VStack {
						Button("Open Viewer") {
								if let url = URL(string: "myappname://viewer") {
										 openURL(url)
								}
						}
						Text("Hello, world!")
				}
				.padding()
		}
}
struct Viewer: View {
		var body: some View {
				Text("Viewer")
		}
}
In your App add another WindowGroup for your viewer and set it to enable handling of external launch events (an internal event in our case).
@main
struct GroupDefaultsTestApp: App {
		var body: some Scene {
				WindowGroup {
						ContentView()
				}
				WindowGroup("Viewer") { // other scene
						Viewer()
				}
				.handlesExternalEvents(matching: Set(arrayLiteral: "*"))
		}
}
Now in Project->Info->URL Types type in myappname in the URL Schemes field (and the identifier field too) to register our app with the system.
Now run your app and click the button and it should open a new window!
Post
Replies
Boosts
Views
Activity
We don't use @State for publishers. Try this instead:
struct ContentView: View {
		//		@ObservedObject var auth = AuthModel()
		let publisher = UserDefaults.standard.publisher(for: \.userValue)
		var body: some View {
				 Text("hello!")
				 .onReceive(publisher) { newUserValue in
						 print(newUserValue)
				 }
		}
}
However if you actually want to use the value in your view then it is simply:
struct ContentView2: View {
@AppStorage("userValue")
		private var userValue = ""
var body: some View {
Text("hello! " + userValue)
}
}
Another way:
@FetchRequest var filteredItems: FetchedResults<Item>
init(filter: String) {
_filteredItems = FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "title BEGINSWITH %@", filter))
}
Or you could pass a fetch request in from superview and it could even be @State there. Turn on SQL debugging and check how often you are hitting the database. I wish Apple would help us out here.
Testing units together is called integration testing. Just add another instance to the PersistenceController that uses an in-memory store the same as the preview instance. You can make methods to add sample data and call them depending on what you would like in each.
Have you tried BGTaskScheduler - https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler?language=objc?
I noticed this too with iOS 14b8, please submit a feedback referencing mine: FB8689302 A TabView’s tab onAppear is erroneously called when the tab disappears when another is selected
Also a problem with TabView and onAppear on iOS 14b8
FB8689302 A TabView’s tab onAppear is erroneously called when the tab disappears when another is selected
If it only appears on device then it is important to know what version of iOS your device is running. I can reproduce a similar problem on iOS 14b8 is that what you tried?
My Feedback is FB8689302
A TabView’s tab onAppear is erroneously called when the tab disappears when another is selected
And I referenced your feedback which sometimes helps get it resolved quicker.
The header comment states List selection binding (when not in editing mode) is Mac only. I'd love to hear why!
class PeopleDataModel: NSObject, ObservableObject, NSFetchedResultsControllerDelegate {
		private lazy var fetchedResultsController: NSFetchedResultsController<Person> = {
let frc = NSFetchedResultsController<Person>(fetchRequest: Person.sortedFetchRequest(), managedObjectContext: PersistenceController.shared.container.viewContext, sectionNameKeyPath: nil, cacheName: nil)
frc.delegate = self
				try! fetchedResultsController.performFetch()
return frc
		}()
		public var people: [Person] {
				return fetchedResultsController.fetchedObjects ?? []
		}
		func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
	 objectWillChange.send()
	}
}
struct ContentView : View {
		@StateObject var peopleDataModel = PeopleDataModel()
Or instead of the people getter we could set the fetchedObjects to a @Published property, I'm not sure which is best yet.
If you need to change the fetchRequest, call a fetch(...) method from onAppear, and set the predicate or new sort descriptor and call performFetch there instead. e.g.
func fetch(name:String){
objectWillChange.send()
fetchedResultsController.fetchRequest.predicate = NSPredicate(format: "name = %@", name)
try! fetchedResultsController.performFetch()
}
.onAppear() {
peopleDataModel.fetch(name: name)
}
It's the wrong way round, try:
let newScoringPlay = ScoringPlay(context: currentContext)
// Set attributes and relationships ...
save(currentContext) // Calls a helper method with a do/try/catch pattern to save the context
No need to call insert, that's what the init with context does.
In the App struct you can access the persistent container to set the viewContext on the Environment like this:
import SwiftUI
import CoreData
@main
struct MasterDetailApp: App {
	@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
		
		var body: some Scene {
				WindowGroup {
						ContentView().environment(\.managedObjectContext, appDelegate.persistentContainer.viewContext)
				}
		}
}
I'll make a guess you are implementing your App struct as if it were a class and accidentally creating a new instance of NSPersistentCloudKitContainer every time SwiftUI recreates the struct.
You could access the persistent container lazy property through a @UIApplicationDelegateDelegateAdaptor so it is only created once.
import SwiftUI
import CoreData
@main
struct MasterDetailApp: App {
	@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
		
		var body: some Scene {
				WindowGroup {
						ContentView().environment(\.managedObjectContext, appDelegate.persistentContainer.viewContext)
				}
		}
}
I noticed that when using the Simulator if you turn Wi-Fi off then do a context save you see lots of output including 12 instances of "The Internet connection appears to be offline." but if you turn Wi-Fi back on and then after a context save it never tries to sync again, even if you suspend and resume the app which is how you can usually trigger a sync on the Simulator.
Perhaps your problem is also caused by the device losing its network connection and syncing getting stuck?
Xcode Version 12.0 beta 3 (12A8169g)
iPhone SE (2nd generation) iOS 14.0 (18A5332e)
Have you tried destroying the store and re-loading?
persistentContainer.persistentStoreCoordinator.destroyPersistentStore