Swift Realm, load the pre-populated database the right way?
Yeah, your logic is correct. Every time this code gets executed, the default Realm file in the Documents directory is deleted and replaced with the static copy that came with the app bundle. This is done by design in the Realm sample code in order to demonstrate the migration process each time the app is launched.
If you only want that to happen one time, the easiest way to do it would be to check beforehand to see if a Realm file already exists at the default path, and then perform the copy only when it isn't already there. :)
let alreadyExists = NSFileManager.defaultManager().fileExistsAtPath(defaultPath)
if alreadyExists == false && let bundledPath = path {
print("use pre-populated database")
do {
try NSFileManager.defaultManager().removeItemAtPath(defaultPath)
try NSFileManager.defaultManager().copyItemAtPath(bundledPath, toPath: defaultPath)
} catch {
print("remove")
print(error)
}
}
Best Way to Build a Pre-Filled Realm Database
You could create a simple app (maybe a new target within the same Xcode workspace) only to pre-fill a Realm database, run it on the simulator, open the sim folder on your system and drag the Realm file from there into your main project.
Then, when the main app launches, copy the file from your bundle to a more appropriate location (such as the documents folder)
How to Preload 'Realm' DB into iOS swift3?
The piece of code below works perfectly for me to load a preloaded Realm instance during the first launch of my app:
let defaultPath = Realm.Configuration.defaultConfiguration.fileURL?.path
let path = Bundle.main.path(forResource: "default", ofType: "realm")
if !FileManager.default.fileExists(atPath: defaultPath!), let bundledPath = path {
do {
try FileManager.default.copyItem(atPath: bundledPath, toPath: defaultPath!)
} catch {
print("Error copying pre-populated Realm \(error)")
}
}
Pre-populated Realm isn't being copied on the first launch
You have an issue in another place ;)
The first try
trowing an exception at first run because you don't have this file yet. And copyItemAtPath
not calling after that. So you have an empty default realm file with no data after all of this.
func openRealm() {
let defaultPath = Realm.Configuration.defaultConfiguration.path!
if let v0Path = bundlePath("names.realm") {
if NSFileManager.defaultManager().fileExistsAtPath(defaultPath) {
do {
try NSFileManager.defaultManager().removeItemAtPath(defaultPath)
print("Remove old file")
} catch {
print("Wasn't removed")
}
}
do {
try NSFileManager.defaultManager().copyItemAtPath(v0Path, toPath: defaultPath)
print("Copied.")
} catch {
print("Wasn't copied.")
}
}
}
Related Topics
Blue Highlighting/Focus Ring on Catalyst App
Get Just the Date (No Time) from Uidatepicker
Swift Error: Guard Body Must Not Fall Through
How to Use .Svg Images in Swiftui
How to Block Users on Firebase in a Social Media App? for iOS
Understanding the Removerange(_:) Documentation
Swift 4 Codable - API Provides Sometimes an Int Sometimes a String
Select All Text in Textfield Upon Click Swiftui
Swift String VS. String! VS. String
How to Have a Swift Protocol Without Functions
Multi-Component Picker (Uipickerview) in Swiftui
Swift Standard Documentation Comment
Can't Pass Date to Nspredicate(Format: ...) Without "As Cvararg"
Open a Viewcontroller from Remote Notification
Spritekit - Create at Random Position Without Overlapping
Vapor Toolbox Broken After Upgrading Swift
How to Make Alphabetically Section Headers in Table View with a Mutable Data Source