Search code examples
iosswiftrealm

Cannot write to my prebundled realm file


Cannot convert value of type '() -> ()' to expected argument type 'Data?

this is what I get for this line: try realm.writeCopy(toFile: url) {

I have no idea how to write to the opened prebundled database

        for question in questionBank! {
            do{
                let url = URL(fileURLWithPath: Bundle.main.path(forResource: "questions", ofType: "realm")!)
                try realm.writeCopy(toFile: url) {
                    question.answered = false
                    print(question.answered)
                }
            } catch {
                print("ERROR ZEROING QUESTIONS")
            }
        }

Solution

  • writeCopy(toFile:) is not an alternative to using write. It actually copies your existing .realm file and copies it to the URL supplied as the input argument.

    You need to call a regular write transaction if you want to modify your .realm file and create a copy of the .realm before modifying it if you want to store several versions.

    However, you'll run into troubles soon if you are modifying bundled files since that will break the code signature of your application. If you want to use a prepopulated .realm file from the application Bundle, first copy it to the default location used by realm, then you can simply write to the default realm. If you don't know how to copy a prepopulated Realm file to the default location, have a look at this answer of mine.