|
In the SwiftUI Lifecycle of a Document based app, what is the correct method to insure that a save is performed before the app quits or a scene/window closes? I have a variable for changeCount in myDocument which is updated anytime a change is made to the data. The data is a NSAttrributedString saved in a JSON tree struct coming from an NSTextView using representable. If I select save from the file menu, it saves correctly. If I then quit and relaunch it loads the changed data correctly. But, if I change the data and quit then relaunch, the changes were not saved. Any links, suggestions? There ia a real derth of tutorials and exmaples for MacOS and SwiftUI - I have spent several days and can not find a best practice for this. ANY help would be appreciated. |
|
The SwiftUI app life cycle has a
Replace the Saving Data When Your Swift App Quits I'm not sure how to trigger a save when closing the document window. I tested a SwiftUI document-based app I'm making to see if I could reproduce the behavior you're seeing. I couldn't reproduce the behavior. The changes were saved when I quit the app and closed the window. I looked at the code to see if I was doing anything special in the document struct, but I didn't find any special code. |
|
Thank you for the reply. I do have onChange and it does not trigger when quiting or closing the window in my app. Does your test app have a flat data model or a linked tree model? I think part of my problems is my data models is a tree structure with a link between the structs. I have not changed "much" of the basic Document App code. I added coding the NSAttributedString and that is pretty much it. I "think" the issue is my data model. I have 2 structures which define a tree structure. I am making an sort of word processor for speicific applications and I am making the UI similar to the XCode. The first structure containst the definition of a project and a link to a second structure which contains a tree organization of sections, each section can contain pages/AttributedStrings. The editor text view is a NSTextView using NSViewRepresentable. I think this is part of the problem. I think the edits which change the text property of the Sidebar struct (below) do not get recognized as a change to the document. Meaning the document does not observe the sidebar data which is linked to in the Project. So, no saves are being triggered automatically. The part that confuses me, is that a regular open or save from the menu does work, so it would seem the document should "know" about the text property. I added a "change count" to the project structure but, that did not help. I agree if I just make an app using the document template it works properly. If I knew what was triggering the Document code to detect a change and cause a save, I could do that but I can't find any suggestions.
|
|
I have not used a tree structure with a document-based SwiftUI app so I can't say if the tree structure is the cause of your problem. In the SwiftUI app I was developing, I had a menu to insert Markdown tags in a text editor, and the document wouldn't be marked as changed until I added a dummy You showed a bunch of code for several classes, but you didn't show the code for your document data structure. What is the relationship between the document data structure and the classes you posted? You're going to have a tough time finding a solution to this. Apple does not provide much documentation on SwiftUI document-based apps. Most iOS developers do not develop document-based apps so there's not many articles and tutorials about them. |
|
I also added a "changed" var to the Document structure. It didn't help. I was going to change it to a non-Static. I tried static just to make it easy to test. Next I will take out the stataic keyword and try accessing it otherwise. The Project var is the main struct in my model, which contains a link (shown above) to the sidebar structure which is a nested tree structure. So, it all links back to the Project, here. As I was writing this and looking at this struct, I got to thinking. When you have linked data, the link (read here) is never changed (well, it can, but that is not my problem yet.) It is the data that the link points at. I expect that is the root of the problem. That FileDocument is happy with the link having not changed. I am going to try to create a simple test to see if linked to data can trigger a save in a simple case with no nesting.
|
|
I was able to duplicate this error using the SwiftUI Document Template. I simply replaced the "text" var in the document with a structure containing a text var. And it no longer detected changes in the text. I debugged that and it turned out I was not init'ing the structure correctly. |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.