Realm-cocoa: How to replace default Realm with another Realm (in the bundle)?

Created on 17 Mar 2016  路  5Comments  路  Source: realm/realm-cocoa

I've been reading a bunch of Stack Overflow posts and the documentation, but I can't seem to achieve the results I'm after. :grimacing:

Goals

I am building a game where most of the in-game content, add-ons, etc are being stored in Realm. I would like to pre-populate a Realm (that's being stored in the app's bundle) with this data and copy all of it to the default Realm when the app is launched for the first time.

  • Content will have an unlocked value that will determine if the user can see that bit of content.

Expected Results

  • Pre-build all game content in Realm.
  • Copy all data to the default Realm on first launch.

    Actual Results

  • Nothing right now, Not sure where to start.

    Version of Realm and Tooling

  • Realm v0.98.5

  • Xcode Version 7.2.1 (7C1002)
  • OS X 10.11.3 (15D21)
  • RealmSwift on iOS
  • Carthage

I'd appreciate a push in the right direction. :dancer:

T-Help

Most helpful comment

let path = Realm.Configuration.defaultConfiguration.path!
// delete any previously created Realm files
do {
  try NSFileManager.defaultManager().removeItemAtPath(path)
} catch {}
_ = try! Realm()
print("empty Realm created at path: \(path)")

All 5 comments

Have you read the Bundling a Realm with an App section of our docs? I recommend you start there, and if you run into problems, let us know what went wrong.

Thanks @jpsim! I'll take a look and see how far I get. :+1:

The Bundling a Realm with an App guide got me pretty far in achieving what I wanted, but I'm having issues figuring out how to create a fresh/empty *.realm file. Could you point me in the right direction?

let path = Realm.Configuration.defaultConfiguration.path!
// delete any previously created Realm files
do {
  try NSFileManager.defaultManager().removeItemAtPath(path)
} catch {}
_ = try! Realm()
print("empty Realm created at path: \(path)")

Thanks again, @jpsim! :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

i-schuetz picture i-schuetz  路  3Comments

dennisgec picture dennisgec  路  3Comments

fadylateef picture fadylateef  路  3Comments

xspyhack picture xspyhack  路  3Comments

ishidakei picture ishidakei  路  3Comments