TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: HowTo remove MacOS App Data when started from Xcode

Forums > macOS

Hello,

after creating some small IOS apps with SwiftUI and SwiftData, I am now trying to write a small test app for MacOS. I also use SwiftData to keep data persistent. The first version works so far, but now I want to customize my @Model class. I noticed that I don't know a way to completely delete a MacOS app started via XCode. If I stop the app, delete the data from "/Users/ralfb/Library/Developer/Xcode/DerivedData/SyncDataV2... " and then restart it, the items that I added previously and that are stored in SwiftData are still there.

Question: How can I completely delete a MacOS app launched from Xcode, including the SwiftData data, from the system?

I am grateful for any tips :-)

Regards, Ralf

   

Hey, perhaps this can help. In Systemsettings -> General -> Memory is a group "Developerteam"

Lookm inside this entry to delete Caches, p4erhaps also for the SwiftData

Regards, Marcel

   

Hey Marcel, Thanks for the hint, but unfortunately this will not delete SwiftData :-/

Regards, Ralf

   

Hello,

perhaps some background to my question. I have a SwiftData model and would now like to add another variable. When I start the app afterwards, the app crashes because the SwiftData DB still exists with the old model.

Maybe there is another way to extend the model?

Regards, Ralf

   

   

Hi Marcel,

The post describes my problem, but unfortunately the solution was not published. The note with @Relationship is interesting, but unfortunately I don't have the idea of how I would have to implement it.

Anyways, thank you very much for your great support!

Regards, Ralf

   

Hello,

I have now tested a bit again, and even though I still haven't found a way to completely delete the app including the SwiftData database, it seems to work now that I can add more properties to the model. I'm not sure why I got an error on the previous attempts, maybe I did something different now. Here are the steps that worked for me:

  1. added new variable in @Model class.
  2. deleted initializer (init)
  3. have the initializer recreated via Xcode (enter init and then XCode will suggest it)
  4. where a new item is added, the new variable is added - Xcode draws attention to the missing arguments during the build

I was then able to build the project and start it without any problems.

Nevertheless, I would be interested in knowing how to delete the SwiftData database, as with IOS.

Greetings, Ralf

   

Hello,

After doing some more research and testing, I would like to share my findings here in the hope that it might help some of you.

Here are my findings:

  1. it is possible to programmatically delete the contents of the database:
modelContext.container.deleteAllData()

Then close and restart the app and no more content will be available.

  1. the path to the SwiftData SQLlite database

The following command outputs the path to the Swift DB:

let theURL = NSPersistentContainer.defaultDirectoryURL()
print("**** URL: \(theURL) ****")

What I have not yet understood is that if I initialise SwiftData as described by Apple and start my app, add items, then the above-mentioned path is still without content. Nevertheless, the app works without any problems and the data is persistent.

However, it is possible to define the path to the database in the code when initialising the ModelContainer.

var sharedModelContainer: ModelContainer = {
        let schema = Schema([
            Script.self,
        ])

        let baseURL = NSPersistentContainer.defaultDirectoryURL()
        let storeURL = baseURL.appendingPathComponent("ralfb-SyncDataV2.sqlite")

        let modelConfiguration = ModelConfiguration(schema: schema, url:  storeURL)
        do {
            return try ModelContainer(for: schema, configurations: [modelConfiguration])
        } catch {
            fatalError("Could not create ModelContainer: \(error)")
        }
    }()

The path is defined via baseURL and storeURL, and when I then start the app I find the following files under the path (baseURL):

ralfb-SyncDataV2.sqlite
ralfb-SyncDataV2.sqlite-shm
ralfb-SyncDataV2.sqlite-wal

If I delete this data and the DerivedData, then all app data, including the persistent data, is deleted.

Regards, Ralf

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!

Reply to this topic…

You need to create an account or log in to reply.

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.