Storing Window Settings Experiment don't work for me

Hi,

Trying the experiment of page 106 on section Storing Window Settings, when I reopen the app, it only open one window. I have tried to do what the note says and no more windows appear either.

I’m not sure why this happens. Can you give me some ideas?

Thanks in advance

Hi,

Download the book materials and run the project from the 04-using-a-table-view/projects/challenge folder. Does that work for you?

When you create the second window, do you see both windows listed in the Window menu?
When you restart the app, what does the Window menu show then?

Are there are error messages in the Xcode console?

If Xcode is misbehaving, cleaning the build folder sometimes helps, so press Shift-Command-K and try again.

1 Like

Hi Sarah,

@SceneStorage is not working for me either.

When my version didn’t work, I built the Challenge folder project from the downloaded materials. I ran the app, resized the Event grid window, created a new window and chose Deaths and Table and resized it. Both windows are listed in the Window menu. Then I quit the app, Control-Q, and reran it from Xcode. I just get a single events window, grid view, but with the D and T new size and location. Same after I cleaned the build folder and rebuilt. The Window menu lists a single window.

Then I used a single window, resized, picked deaths and table, quit, and reran, and I get an events grid, not a death table, with the new size. The Window menu lists a single window.

Then I told my Mac to keep the OnThisDay app in the dock and tried again quitting and rerunning from there instead of Xcode. It remembers the size and location of a single window, but not grid vs table selection nor the sidebar selection. If I get a second window with File → New Window, adjust, quit, and rerun from the dock. I get a single event grid window sized and positioned as the second window above.

Only console messages, which I always get, seems unrelated:

2022-05-01 10:58:19.709062-0500 OnThisDay[33810:1194206] [logging] volume does not support data protection, stripping SQLITE_OPEN_FILEPROTECTION_ flags*

I suspect some strange preference or security setting on my iMac, but have no clues…

Xcode 13.3.1; macOS 12.3.1; iMac (Retina 4K, 21.5-inch, 2017)

-Steve

I don’t have an answer for you, but I have two questions:

  • Are you running the app from your boot drive or is it on an external drive?
  • Do you have File Vault enabled?

The only suggestion I have is to do a full reset of Xcode and the app’s stored data in case there is something strange there.

  • Quit the app and close the Xcode project.
  • Open Terminal and use this command to delete Xcode’s derived data: rm -rf ~/Library/Developer/Xcode/DerivedData
  • In Finder, open the Go menu and hold down Option. This will let you select Library. In the Library folder, open Containers and look for any folders called OnThisDay (there may be more than one). Delete all of them.

Then try building and running the app again from the Xcode project.

Hopefully this will fix it.
Sarah

1 Like

Thank you Sarah!

The full reset of Xcode worked! I had LOTS of stuff in DerivedData. I had two OnThisDay folders in the Containers folder/directory. This will be another thing I will try in the future when things go crazy.

To answer your questions: I run the app out of my boot drive which is a funky Apple fusion drive (part SSD; part spinning disc). I do have File Vault enabled. There is a pretty big security hole in macOS without that – if File Vault is not enabled anyone with physical access to the machine can get root privileges without a password.

I also have a second monitor. Several public apps get confused by that. Some open progress or alert windows at the boundary between the iMac display and the 2nd display. Or in the middle of the 2nd display and then open the main window on the iMac display.

-Steve

That’s great. Good work persevering through the issue.

Clearing DerivedData can often fix Xcode problems, so it’s a good one to remember.

A second monitor should not cause any issues, but I thought File Vault might be blocking something.

Sarah

1 Like

Hi Sarah,

same issue here. I’m using the chapter 4 “final” OnThisDay project from GitHub sources. Unfortunately the “reset everything” steps from above did not fix the issue.

Looking at the contents of /Users/ml/Library/Containers/com.raywenderlich.OnThisDay/Data/Library/Preferences/com.raywenderlich.OnThisDay.plist (which I will attach), it seems that AppStorage stuff is written correctly, but SceneStorage is not. Any ideas where to look, how to fix it? Bug in SceneStorage?

Thanks for any hints/help! And thanks for the great book. I was so happy to see a macOS development book again! :slight_smile:

Cheers, Michael
com.raywenderlich.OnThisDay.plist.zip (1.1 KB)

Thanks Michael, I’m glad you are enjoying the book.

I can’t see why SceneStorage isn’t working for you. In your plist, I can see entries with a name that includes AppWindow-1. My plist has AppWindow-1 and AppWindow-2 entries and when I quit and restart the app with the two windows open, they re-appear in the same places and with the same settings.

Just a few suggestions you could try:

  • Quit the app, delete all OnThisDay folders in ~/Library/Containers and try again.
  • Wait a few seconds after making a change before quitting.
  • Quit the app using its own Quit menu and not Xcode’s Stop button.

Let me know if any of this works for you.

Sarah

1 Like

Hello Sarah,

unfortunately no luck. Same behavior: AppStorage works fine - SceneStore not at all.

After deleting the App-Container I saw this message in the console:

OnThisDay[20411:10689580] [User Defaults] Couldn't read values in CFPrefsPlistSource<0x600003f60c80> (Domain: com.raywenderlich.OnThisDay, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: Yes): accessing preferences outside an application's container requires user-preference-read or file-read-data sandbox access

It appeared only on the first launch of the app. Thinking there might be an issue with the preferences deamon I restarted the computer and the message would not show up again, even after deleting the App-Container. But SceneStorage still does not work.

Another thing I just noticed (since you mentioned you have the settings for two windows in your plist):
When I open two windows only one (the first?) will be restored, with the latest position/size changes no matter to which window they were made.

Running this using the latest macOS/Xcode on an M1 MacBook Air. Which shouldn’t make any difference, but who knows!? :wink:

Cheers, Michael

That console message appears if there are no preferences written yet of the app, so that’s as expected. But I can’t work out why AppStorage works for you but SceneStorage doesn’t.

I too am running the latest macOS & Xcode on an M1 iMac, so that shouldn’t be the issue.

Are you using a Standard account or an Admin account?

Sarah

1 Like

Still a mystery ;-):

I am using an Admin account.

Not sure if AppStorage really works 100% for me, because if I have two windows open they do not re-appear and in the plist only AppWindow-1 is stored. Whereas, if I interpret your comment correctly, both windows should re-appear and a second AppWindow entry should be in the plist.

In the meantime I wrote the most minimalist test app (see below) and tried it on a different machine, with a different user. Unfortunately with the same, wrong behavior (the state of the toggle button is not retained across app launches).

import SwiftUI

struct ContentView: View {
  @SceneStorage("Toggle") var onOrOff: Bool = false

  var body: some View {
    VStack {
      Text("Hello, world!")
        .padding()
      Toggle("Will it work?", isOn: $onOrOff)
        .padding()
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

Thank you, Michael

Hi,

update (Spoiler: Still no success):

To make sure that this is not an issue with my user/macOS installation, I re-installed macOS on a MacBook and only installed the latest Xcode onto it: Same behavior using the ContentView coding from above. Also: When changing the SceneStorage to AppStorage it works just as expected, but well I’d actually need SceneStorage.

So right now I am a bit puzzled how this can work for anyone, since I do not seem to do anything special with regards to my installation and I can’t see anything wrong with the code either!?

Any ideas what else I can try?

Thanks, Michael

Hi Michael,

I’ve just tested your minimalist app and I made an interesting discovery.

If I quit the app using Command-Q or its Quit menu item, the setting is always restored correctly when I re-run from Xcode.

But, if I use Xcode’s Stop button, the setting is not always stored and restored.
This applies to window locations too, so it’s not just code we wrote.

Could this be your issue?

Sarah

Hi Sarah,

thanks for testing. Unfortunately this is not the issue.
I used Cmd-Q. I even compiled and notarized (because why not ;-)) the test app, but got the same result when I ran the app outside of Xcode.

Currently I have tested this on three different machines and additional one colleague of mine tested it as well. Each time nothing was saved when using @SceneStorage. When using @AppStorage instead it worked, though. So it seems that writing to the preferences file is generally possible.

It’s a mystery.

Cheers, Michael

Hi Michael,

I’m sort of making progress, in that I have managed to make @SceneStorage fail on two different MacBooks but it still always works on two different iMacs.

On the MacBooks, the test app doesn’t even record when multiple windows were open, so this is not just a problem with the @SceneStorage property, but with the entire window settings storage system.

I’ll keep looking for answers, but right now, it’s looking like a MacBook bug.
Where all the computers you tested MacBooks?

1 Like

I’ve filed a feedback report at Apple (FB10021846) and published my findings and sample app at https://github.com/trozware/scene-storage-bug-demo.

Hi Sarah,

yes, thats’s exactly the same behavior I am seeing: @SceneStorage not working and only the attributes for one window are remembered (the last one changed if more than one is open).

I filed the issues as FB10011754 (for the @SceneStorage issue) and FB10011751 for the issue with the window attributes. I will reference your FB in my FB’s as well.

The machines on which I saw the issues were:
M1 MacBook Air, Intel MacBook Pro, Intel MacBook, M1 Pro MacBook.

So for now it’s all MacBooks, but those are also the only machines I was able to test on. So I am not sure, if that counts as some kind of evidence :-).

I will update here, once I stumble across some new information.

Cheers, Michael

See my note on Chapter 9. For me, Finder is not listing all the folders in the Containers folder. So, maybe you need to use Terminal to locate and delete all the OnThisDay folders. And maybe that holds for DerivedData, too.

Great. The more FBs the better.

Meanwhile, someone reported that the beta of macOS 12.5 fixed this. I tried and it worked … sometimes.

So I would hope a fix is on the way. Just understand that it isn’t your fault and it isn’t my fault.

HI Sarah,

so Apple got back to me and asked, if “Close All Windows on Quit was unchecked in System Preferences > General?”. It was checked in my case (and this seems to be the default, since it was also checked on the freshly installed machine I tried). By unchecking it @SceneStorage works as expected.

Well, I think from a software developers perspective you could argue, that this is the correct behavior. I think, though, it’s tough to explain this behavior to any “regular” user. Makes me wonder, if it is worthwhile to use @SceneStorage for any kind of state restoration.

At least I think the mystery is solved for now :-).

Cheers, Michael

2 Likes