UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Advice on how to debug a strange bug.

Forums > SwiftUI

Hi All,

I hope someone maybe able to help me track down a mysterious bug that exists in my app when running on device only.

Basically, my app is very simple in that it saves its data to the users documents directory and on the simulator everything works as expected, it even works on device as you would expect too, however at some point of the night the data saved on device resets and I lauch the app to a completely blank screen... strange thing is this DOES NOT happen on the simulator at all.

My question is this, how would I best debug this kind of scenario, if it were a bug on the simulator too then maybe it'd be easier to debug but it never occurs on the simulator and running the app on device through Xcode debugging doesn't reveal much to me either as it functions as expected on every run and when the bug happens at some point of the night the app launches as you'd expect for a clean install on every launch after.

Any help and advice would be very much appreciated.

1      

Hi All,

Quick update to my above question, I now have an error to report regarding this issue and it's one that I am struggling to understand. As this issue only occurs on device I programmed in a debug option to capture the error and report to me and the error was: "The file "SavedCounts" couldn't be opened because you don't have permission to view it" this is strange as it's my own device and I cannot for the life of me understand where this error is coming from, or how to solve this issue.

Now as I know the error is coming from and what it's related to, here is my loading code.

init() {
        do {
            let savePath = getUserPath().appendingPathComponent("SavedCounts")
            let data = try Data(contentsOf: savePath)
            items = try JSONDecoder().decode([Item].self, from: data)
        } catch {
            print(error.localizedDescription)
            errorString = error.localizedDescription
            items = []
        }
}

func save() {
        do {
            let savePath = getUserPath().appendingPathComponent("SavedCounts")
            let data = try JSONEncoder().encode(items)
            print("Saving")
            try data.write(to: savePath, options: [.atomic, .completeFileProtection])
        } catch {
            print("Unable to save due to an error!")
        }
 }

 extension FileManager {

    static var documentsDirectory: URL {
        let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)

        return path[0]
    }

}

As you can see, this is all pretty standard code and used by many, can anyone help or even give more context into why this is happening please?

Thanks in advance!

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.