Coder Social home page Coder Social logo

Comments (6)

jessesquires avatar jessesquires commented on June 2, 2024

Thanks for the report @peterkos!

It's hard to say what's wrong without more context. Is it possible that something else in your app could be modifying the values for these keys? Are you sure there wasn't a corrupted plist involved?

All of the scenarios you described (defaulting bool to false, crashing for non-nil string) are unit tested and (I think) also demonstrated in the Example project in the repo.

Can you reproduce this in the unit tests, example, or in another project outside of your app?

from foil.

peterkos avatar peterkos commented on June 2, 2024

(I made a standalone project but am still trying to find the repro case 😅)

Some extra context: this is for a macOS app w/ SwiftData + sandboxing. When I try deleting the .plist in ~/Library/Containers/[appname]/Data/Library/Preferences/[appname].plist, I get the following error:

// x50-ish times
Couldn't read values in CFPrefsPlistSource<0x6000014d45a0> 
(
  Domain: me.peterkos.[appname], 
  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

// warning
Not updating lastKnownShmemState in CFPrefsPlistSource<0x6000029d0090> (Domain: me.peterkos.Cabinette, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: Yes): 0 -> 20132

The app only calls into my AppStorage class in two places (the App.swift and a preferences window), commenting the other use out results in same crash. This logic is for determining which container to load for SwiftData, maybe it's something with that? (will keep digging)

Edit: It happens regardless of using the standard user defaults, or instantiating my own

from foil.

peterkos avatar peterkos commented on June 2, 2024

This is the nil-causing line: it's unable to convert the String value? Hmm.

CleanShot 2024-04-04 at 13 21 55@2x

from foil.

peterkos avatar peterkos commented on June 2, 2024

Aha!

Yes so -- Previously, I changed the type of the didMigrate var from a Bool to a String, which the above fetch<T> code wasn't able to convert because it was a literal number (from Bool's NSNumber encoding?).

I assumed deleting the prefs .plist in my container was the only place the key was stored (still unsure on that one) -- and that changing the type of the only variable would overwrite the previous instance, if it had the same name. Maybe that could be a feature? (Or if this is expected behavior, a defined error message vs. force-unwrapping)

The fix was to delete the old Bool-typed key from standard UserDefaults:

> defaults delete [appname] didMigrateFromv_0_2_to_0_3_VersionedSchema

from foil.

jessesquires avatar jessesquires commented on June 2, 2024

@peterkos glad you got it figured out! 💯

When I try deleting the .plist in ~/Library/Containers/[appname]/Data/Library/Preferences/[appname].plist, I get the following error

I'm not certain, but I think you might see some unexpected behavior by doing this. Instead, I would probably uninstall and reinstall the app.

Previously, I changed the type of the didMigrate var from a Bool to a String, which the above fetch code wasn't able to convert because it was a literal number (from Bool's NSNumber encoding?).

Yes, something like this is what I anticipated was happening. 😊

I assumed deleting the prefs .plist in my container was the only place the key was stored (still unsure on that one) -- and that changing the type of the only variable would overwrite the previous instance, if it had the same name. Maybe that could be a feature? (Or if this is expected behavior, a defined error message vs. force-unwrapping)

The .plist in your container should be the only one. However, my suspicion is that you were probably running your app from Xcode as well? If that's the case, then your app binary and container are in Xcode's DerivedData/. So then deleting the plist at ~/Library/Containers/[appname]/Data/Library/Preferences/[appname].plist would not have any effect. The plist in ~/Library/Containers/... exists for an app that is properly installed on your mac. (So you'd need to export the app archive from Xcode, install it, etc.)

from foil.

jessesquires avatar jessesquires commented on June 2, 2024

Regarding Foil, there isn't really anything we can do at the library level here, unfortunately. You can misuse UserDefaults directly in exactly the same way.

In the future, for your debugging purposes, instead of using terminal to run defaults delete ..., you can use the UserDefaults API directly in your app, by calling removeObject(forKey:). Just call that during app launch before anything else to "reset" UserDefaults.

from foil.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.