Restore access to DB from bookmarked URL in SwiftUI

I have the following code to bookmark URLs for future access:

 private func checkForSQLiteFiles(at containerURL: URL, appName: String) {
    do {
            viewModel.connect(strConnect: selectedSQLiteFile!.path)
            viewModel.fetchAllTables()
            
            // Save selected SQLite file to UserDefaults
            UserDefaults.standard.set(selectedSQLiteFile, forKey: "selectedSQLiteFile")
            saveBookmark(for: selectedSQLiteFile!, key: "SQLiteBookmark")
        }
    } catch {
        errorMessages.append("Error")
    }
}


func saveBookmark(for url: URL, key: String) {
    do {
        let bookmarkData = try url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
        UserDefaults.standard.set(bookmarkData, forKey: key)
    } catch {
        print("Failed to create bookmark for (url.path): (error)")
    }
}

The code above works fine. BTW I’m getting access using NSOpenPanel to files/directories that I selected.

In another place, I have code to restore URLs from bookmarks, try to connect, and request DB again using the same file I used before.

private func openExistingProject() {
    if let sqliteURL = resolveBookmark(for: "SQLiteBookmark") {
        if sqliteURL.startAccessingSecurityScopedResource() {
            // Check if file is readable and exists
            if FileManager.default.isReadableFile(atPath: sqliteURL.path) {
                print("SQLite file is readable at path: (sqliteURL.path)")
                
                // Proceed to connect and load entities
                viewModel.connect(strConnect: sqliteURL.path)
                viewModel.fetchAllTables()
            } else {
                print("Failed to access the SQLite file at (sqliteURL.path)")
            }
        } else {
            print("Failed to access the security-scoped resource for (sqliteURL.path)")
        }
        
        defer { sqliteURL.stopAccessingSecurityScopedResource() }
    } else {
        print("No valid bookmark for SQLite file")
    }
}

func resolveBookmark(for key: String) -> URL? {
    if let bookmarkData = UserDefaults.standard.data(forKey: key) {
        var isStale = false
        do {
            let url = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
            if isStale {
                print("Bookmark data is stale for (url.path)")
            }
            return url
        } catch {
            print("Failed to resolve bookmark for key (key): (error)")
        }
    }
    return nil
}

But when I call fetchAllTables() in ViewModel:

class SQLiteEntityManager: ObservableObject {
    @Published var isConnected: Bool = false
    private var db: Connection?
    
    func connect(strConnect: String) {
        // Define the path to the SQLite database file
        let dbPath = strConnect
        
        // Create a connection to the database
        do {
            db = try Connection(dbPath)
            isConnected = true
        } catch {
            print("Unable to open database. Error: (error)")
        }
    }
    
    func fetchAllTables() -> [String] {
        guard let db = db else {
            print("Database is not connected")
            return []
        }
        
        do {
            let tables = try db.prepare("SELECT name FROM sqlite_master WHERE type='table'")
            return tables.map { "($0[0]!)" }
        } catch {
            print("Error fetching tables: (error)")
            return []
        }
    }
}

it fails with "Error fetching tables: access denied (code:23)". But it still connects successfully without any issues to that DB using that bookmarked URL.
Error happens only when I try to do db.prepare(“…..”).

Any advices would be appreciated

3

Try to check race condition issue, I think you should call fetchAllTables() only after establishing the connection

as an quick test you can try to call func fetchAllTables() just after db = try Connection(dbPath)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật