iOS Development

ios – Why copy db information with 0 bytes in SwiftUI programming?

Spread the love


Code:

import SQLite
import SwiftUI

class CoreDataViewModel: ObservableObject {
    non-public var db: Connection?
    @Printed var allEntries: [String] = []
    @Printed var searchResults: [String] = []

    init() {
        copyDatabaseIfNeeded()
        openDatabase()
        fetchAllEntries()
    }

    deinit {
        closeDatabase()
    }

    non-public func openDatabase() {
        do {
            let documentsURL = strive FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            let finalDatabaseURL = documentsURL.appendingPathComponent("english_grammar_today.db")

            db = strive Connection(finalDatabaseURL.path)
            print("Efficiently opened connection to database at (finalDatabaseURL)")
        } catch {
            print("Unable to open database. Confirm that you simply copied the database file to the paperwork listing.")
        }
    }

    non-public func closeDatabase() {
        db = nil
        print("Efficiently closed connection to database")
    }


    func searchInDatabase(for key phrase: String) {
        guard !key phrase.isEmpty else {
            // If the key phrase is empty, present all entries
            searchResults = allEntries
            return
        }

        do {
            let question = "SELECT entry FROM dictionary WHERE entry LIKE ?"
            searchResults = strive db?.put together(question + " ?", ["%" + keyword + "%"]).map { $0[0] as? String ?? "" } ?? []
        } catch {
            print("Error looking in database: (error)")
        }
    }

    func fetchAllEntries() {
        do {
            let question = "SELECT entry FROM dictionary"
            allEntries = strive db?.put together(question).map { $0[0] as? String ?? "" } ?? []
        } catch {
            print("Error fetching all entries: (error)")
        }
    }

    func copyDatabaseIfNeeded() {
        // Transfer database file from bundle to paperwork folder

        let fileManager = FileManager.default

        guard let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

        let finalDatabaseURL = documentsUrl.appendingPathComponent("english_grammar_today.db")

        do {
            if !fileManager.fileExists(atPath: finalDatabaseURL.path) {
                print("DB doesn't exist in paperwork folder")

                if let dbFilePathPath = Bundle.principal.path(forResource: "english_grammar_today", ofType: "db") {
                    strive fileManager.copyItem(atPath: dbFilePath, toPath: finalDatabaseURL.path)
                } else {
                    print("Uh oh - english_grammar_today.db shouldn't be within the app bundle")
                }
            } else {
                print("Database file discovered at path: (finalDatabaseURL.path)")
            }
        } catch {
            print("Unable to repeat foo.db: (error)")
        }
    }

}

The trail

**DB doesn't exist in paperwork folder**

**Uh oh - foo.db shouldn't be within the app bundle**

**Efficiently opened connection to database at file:///Customers/howiema/Library/Developer/CoreSimulator/Units/E49EA14F-17E0-4F09-8235-2F7F5835095F/knowledge/Containers/Knowledge/Software/46FA47A0-B2DE-4A9C-9346-5DD2B01C6F97/Paperwork/english_grammar_today.db**

**Error fetching all entries: no such desk: dictionary (code: 1)**

After I clicked to run this system, I noticed that the desk dictionary doesn’t exist… And I dunno why…

Dictionary Desk

The hyperlink is image, Why the db file is 0 byte??

/Customers/howiema/Library/Developer/CoreSimulator/Units/E49EA14F-17E0-4F09-8235-2F7F5835095F/knowledge/Containers/Knowledge/Software/46FA47A0-B2DE-4A9C-9346-5DD2B01C6F97/Paperwork/english_grammar_today.db

I wish to copy the db file to that machine file path, containing the construction and knowledge

3 thoughts on “ios – Why copy db information with 0 bytes in SwiftUI programming?

  1. I loved you better than you would ever be able to express here. The picture is beautiful, and your wording is elegant; nonetheless, you read it in a short amount of time. I believe that you ought to give it another shot in the near future. If you make sure that this trek is safe, I will most likely try to do that again and again.

  2. You could never find the words to describe how much I loved you. No matter how beautiful the picture is or how polished your writing is, you read it quickly. To be honest, I think you should give it another chance soon. I will probably try to go on this hike again and again if you make sure it is safe.

Leave a Reply

Your email address will not be published. Required fields are marked *