Realm-cocoa: Preloaded realm file (xcode 7 - swift 2.0)

Created on 24 Nov 2015  路  6Comments  路  Source: realm/realm-cocoa

Hi All,

Any help regarding the preloaded realm file using the latest version of xcode and swift, I have to include a preloaded realm file in my project.

Any suggestion or sample link?

Thanks.

T-Help

Most helpful comment

@rjjohnpatrickcruz There are some explanations for preloaded realm file in our documentation: Other Realms and Bundling a Realm with an App I hope these help.

It's quite easy to bundle preloaded Realm files in your app.

  1. Create data using iOS simulator or Mac app.
  2. Copy data files to your app bundle.
  3. Read data from files in app bundle Note: Realm should be read only, because app bundle does not allow writing.
let config = Realm.Configuration(
    // Get the path to the bundled file
    path: NSBundle.mainBundle().pathForResource("MyBundledData", ofType:"realm"),
    // Open the file in read-only mode as application bundles are not writeable
    readOnly: true)

// Open the Realm with the configuration
let realm = try! Realm(configuration: config)

// Read some data from the bundled Realm
let results = realm.objects(Dog).filter("age > 5")

All 6 comments

@rjjohnpatrickcruz There are some explanations for preloaded realm file in our documentation: Other Realms and Bundling a Realm with an App I hope these help.

It's quite easy to bundle preloaded Realm files in your app.

  1. Create data using iOS simulator or Mac app.
  2. Copy data files to your app bundle.
  3. Read data from files in app bundle Note: Realm should be read only, because app bundle does not allow writing.
let config = Realm.Configuration(
    // Get the path to the bundled file
    path: NSBundle.mainBundle().pathForResource("MyBundledData", ofType:"realm"),
    // Open the file in read-only mode as application bundles are not writeable
    readOnly: true)

// Open the Realm with the configuration
let realm = try! Realm(configuration: config)

// Read some data from the bundled Realm
let results = realm.objects(Dog).filter("age > 5")

Thanks! @kishikawakatsumi I try this.

its working @kishikawakatsumi! Thanks. cheers!

@rjjohnpatrickcruz My pleasure!

@kishikawakatsumi Thanks for the example. By the way, the Realm docs needs updating. Realm.Configuration doesn't seem to have that particular initializer anymore.

Which docs? We use fileURL in all our docs as far as I know.

Was this page helpful?
0 / 5 - 0 ratings