sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.85k stars 365 forks source link

Migration to SwiftyUserDefaults 5.0.0 betas XCode stuck in Indexation #212

Closed imrabti closed 4 years ago

imrabti commented 4 years ago

I followed the migration guide from version 4.0.0 to 5.0.0 and now my project is stuck in indexation, and can't perform a complete build.

sunshinejr commented 4 years ago

Hey @imrabti! Do you happen to have any errors or logs in Xcode maybe? Are you sure it's related to SwiftyUserDefaults? Can you build the project? Can you post more info when you have a chance?

imrabti commented 4 years ago

Hi @sunshinejr I have fixed all the errors in the code related to the migration, unfortunately I’m now able to complete the build of the project it is stuck. I’m using Swift 5.0 and iOS 12.4 is Swift 5.1 required for the new version ?

sunshinejr commented 4 years ago

@imrabti we support Swift 4.2+ so the Swift version shouldn't be a problem. So you can't build the project or? Did you try removing all the cache you had (clean project, then close Xcode, then remove Derived Data) and try again? What package manager do you use? Can you reproduce the issue in an example app so I can take a look?

imrabti commented 4 years ago

I use cocoaspods, I’ll try your advice for deleting cache and Derived Data I hope this will fix the issue. If not will try to put together a example app that have the same issue. Thank you a lot for your support.

sunshinejr commented 4 years ago

@imrabti No problem, this does sound like a problem with tooling but if it happens to be the problem of SwiftyUserDefaults as a library I will for sure try to fix it ASAP. Just need more info so I could try to reproduce & ultimately fix the issue.

gholias commented 4 years ago

I'm also experiencing the same issue How to reproduce:

After that the indexing never finishes and the project is stuck when trying to build

sunshinejr commented 4 years ago

@gholias thanks for the reply! Can you check if specyfing master branch instead of version 5.0.0-beta.2 fixes the issue?

FabianTerhorst commented 4 years ago

I could reproduce it that xcode 11 isn't able to compile following code and will hang forever.

if let value = Defaults[\.yourNullableKey] {
}
sunshinejr commented 4 years ago

@FabianTerhorst what is your environment? (macOS version, Swift version, package manager, your dependencies)? Are you able to create a new project and reproduce the same issue on a clean project?

FabianTerhorst commented 4 years ago

macos 10.14.6, XCode 11.0, Swift 5.X and SwiftyUserDefaults is a dependency of a swift package i have locally. The app target uses the swift package as a dependency that has SwiftyUserDefaults as a dependency. The swift compiler will hang forever. Quickfix is e.g.

let value1 = Defaults[\.yourNullableKey]
if let value = value  {
}

This looks very much like a compiler bug and nothing related to this library.

FabianTerhorst commented 4 years ago

What isn't a compiler bug is that after i workarounded the building the app crashes on runtime with EXC_BAD_ACCESS (code=1, address=0x0) at

public extension DefaultsKeys {
      var dark: DefaultsKey<Bool> { return .init("dark", defaultValue: false) }
}

As soon as i access the key via Defaults[.\dark] .

FabianTerhorst commented 4 years ago
Bildschirmfoto 2019-09-25 um 17 08 27
FabianTerhorst commented 4 years ago

I use beta 3 of SwiftyUserDefaults

sunshinejr commented 4 years ago

That is really weird, got the compiler hang on the if statement right now... Looking at this, what is interesting the dynamic key paths seem to work correctly - can you try using Defaults.dark @FabianTerhorst and see if it works for you too?

FabianTerhorst commented 4 years ago

Using the key paths fixes the compiler step as well, but still produces crash at runtime. I also got one time this log https://github.com/apple/swift/blob/master/stdlib/public/core/KeyPath.swift#L2483

FabianTerhorst commented 4 years ago
Bildschirmfoto 2019-09-25 um 17 26 59
FabianTerhorst commented 4 years ago

I don't have a custom DefaultsAdapter, this crashes as well DefaultsAdapter<DefaultsKeys>(defaults: .standard, keyStore: .init()).dark

FabianTerhorst commented 4 years ago
Bildschirmfoto 2019-09-25 um 17 39 52

It looks like the keyStore isn't initialized at the time of the call.

sunshinejr commented 4 years ago

@FabianTerhorst nice find! I found out that when I replaced a subscript with keyPath for another method, it didn't have problem anymore. Wonder if it's a bug with generic subscripts and keypaths...

Also hasn’t been able to get the crash yet 🤔

FabianTerhorst commented 4 years ago

I haven't understand yet why its crashing when just doing DefaultsKey<Bool>("dark", defaultValue: false) as well.

sunshinejr commented 4 years ago

@FabianTerhorst I think I found the issue (more or less) regarding the if statement. Surprisingly there is a problem with subscripts, when I renamed the subscripts for just keys to be Defaults[key: key] and Defaults[\.keyPath] then the problem with indexing/building was resolved for me.

I'm currently trying to recreate the bug and file a JIRA issue currently but we might need to go with that rename for now in the worst case scenario.

sunshinejr commented 4 years ago

about the crash - did you try cleaning the project, removing derived data and reopening Xcode? seems really strange that it would crash on DefaultsKey<Bool>("dark", defaultValue: false)

FabianTerhorst commented 4 years ago

Yes still crashes at DefaultsKey<Bool>("dark", defaultValue: false) . I removed derived data, closed xcode and restarted macos.

Bildschirmfoto 2019-09-26 um 14 56 09
sunshinejr commented 4 years ago

This is really interesting. Could you try using version 4.0.0 and see if it still crashes on the same line?

I found similar issue to yours in the Swift forums - https://forums.swift.org/t/crash-in-uiimagepickerdelegate-method-after-migrating-project-to-swift-5/25962 Did you try playing with optimization levels?

FabianTerhorst commented 4 years ago

I tested all optimization levels and crash happened on all of them. I just tested with 4.0.0 and crash isn't happening.

sunshinejr commented 4 years ago

@FabianTerhorst this is definitely worrying me. If only I could reproduce the problem I would try tackling this one. Any hints on what might be the culprit?

Also I just reported a Swift bug: https://bugs.swift.org/browse/SR-11529 Will do the workaround with named subscripts for Defaults[key: key] instead of Defaults[key] as it will not affect that much people since you will use Defaults[\.keyPath] or Defaults.keyPath most of the time using Swift 5.+.

sunshinejr commented 4 years ago

@FabianTerhorst I just created a PR with the fix for if statement - can you check if it works for you?

RachitTechnology commented 3 years ago

Thanks for wonderful library. Seeing similar issue with XCode 12.4 , if we add a new file with Defaults[\.Key] , builds takes forever to complete and stuck on loading from cache message. using Defaults[key : key] does it faster. Anything should be changed in code? Using latest version of library

sunshinejr commented 3 years ago

@RachitTechnology if that's 5.0 version, did you try defaults[\.key] instead? we switched to key paths in the latest version

RachitTechnology commented 3 years ago

yes, tried with defaults[\.key]. When added in new swift file, first compilation takes forever to complete. Once compile is complete, however next compile is super fast. Is there way we can capture logs , so if happens with new file, can capture and upload data

rami965 commented 1 year ago

I had the same issue in 5.3.0 but I found that I was using a property within its own getter which is getting a Defaults value. That lead to a recursion which caused EXC_BAD_ACCESS crash. Hope this helps someone.