Answer the question
In order to leave comments, you need to log in
How to get a file from Realm?
I initialize the Realm database like this:
let fileURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: self.identificator)!
.appendingPathComponent("default.realm")
let config = Realm.Configuration(fileURL: fileURL, schemaVersion: 2, migrationBlock: { (migration, oldSchemaVersion) in })
let realm = try! Realm(configuration: config)
Answer the question
In order to leave comments, you need to log in
We make a Data object:
realmURL in my case this is the fileURL from the question.
Save with extension:
toFile
method :
func toFile(fileName: String, extenshion: String) -> URL? {
if let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = directory.appendingPathComponent(fileName + "." + extenshion)
do {
try self.write(to: fileURL)
return fileURL
}
catch {
print("Error writing the file: \(error.localizedDescription)")
}
}
return nil
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question