I'm developing an application for iOS using swift and chose Realm as a database solution for it. I wrote default data in AppDelegate using write/add function from realm docs and it works just fine. So after first launch I have a *.realm file with my initial data. In Realm documentation I found a section called "Bundling a Realm with an App", I add my *.realm file to project and to Build Phases as it written.
And I can't understand what I should do next (and part about compressing a *.realm file). I've tried to understand a code from Migration Example but I don't know Obj-C well.
Please give as clear steps as you can to add *.realm file with initial data to swift ios project and load this data to the Realm db with the first launch.
Work in the enterprise space, I need to open a Realm for each application without reusing Realm across all applications so I put this together for Swift 3.0. Add this function to the AppDelegate.
The code above opens or creates a Realm with the file name of "ApplicationNameGoesHere.realm" based on the appName variable in this example.
place
}
call it in another class like this:
Updating @pteofil's openRealm function for Swift 2.2/Realm 1.0.2:
If you want to open it straight from the bundle location and not bother copying it to the default Realm path, look at the implementation here
And for those that need @pteofil's answer in Objective-c
Implement this function
openRealm
in AppDelegate and call it inIt will copy your realm file that you bundled in the app to the default realm path, if it doesn't exist already. After that you use Realm normally like you used before.
There's also the Migration example that you talked about in Swift.
In Swift 3.0.1 you may prefer this:
(but please be careful with the optionals)
Swift version 3, courtesy of Kishikawa Katsumi: