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

How can I make a SwiftData Backup from iOS

Forums > iOS

@alexn  

I would like to copy the swiftdata database from my Iphone App to make a backup. Where can I find and access these files?

   

@alex is keen to have a backup.

I would like to copy the swiftdata database from my Iphone App to make a backup

I don't have an answer for ye. However, this clue might help?

In January 2024, Stewart Lynch completed a series of YouTube vids on SwiftData.

In his first episode titled SwiftData CRUD Operations Stewart writes a line of code to reveal the location of the SwiftData files. In subsequent lessons he navigates to this folder to make backups whilst testing delete and other functions.

See-> SwiftData CRUD Operations
Locating the database files starts at 6:05.

Keep Coding

Please return here and let us know how you implemented this in your application!

   

@alexn  

Thanks. The video helped me. With these code snippets you can display the location and the existing files. The idea now is to share these files so that you have a backup of the database.

        print(URL.applicationSupportDirectory.path(percentEncoded: false))
        let directoryPath = URL.applicationSupportDirectory.path(percentEncoded: false)
        let fileManager = FileManager.default
        do {
            let fileURLs = try fileManager.contentsOfDirectory(at: URL(fileURLWithPath: directoryPath), includingPropertiesForKeys: nil)

            for fileURL in fileURLs {
                print("\(fileURL.lastPathComponent)")
            }
        } catch {
            print("Cant read Files \(directoryPath): \(error.localizedDescription)")
        }

/var/mobile/Containers/Data/Application/8A7F8ED3-6D04-47BC-8CA2-F6FEA9788721/Library/Application Support/
default.store
default.store-wal
default.store-shm

   

This works for the ios device simulator, but it is not obvious to me how to backup files on a real ios device in view of sandboxing.

   

To back up the SwiftData database from your iPhone app, connect your iPhone to your computer and use software like iTunes, iExplorer, or iMazing to access its file system. Navigate to the folder where the app's data is stored, typically within the "Apps" or "App" folder. Look for the SwiftData database file, usually with a ".sqlite" or ".db" extension, and copy it to your computer.

   

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.