How to Preload 'Realm' DB into iOS swift3?

Viewed 1168

I am trying to implement Realm database in my app , I have realm database with some Preloaded data into it . I have searched through many stack overflow resources but didn't get any success.

So far now , I have done following steps :

  1. Copy the Realm File into my app's bundle
  2. Added following code in the app delegate :

    path = Bundle.main.path(forResource: "data", ofType: "realm")
    var config = Realm.Configuration(fileURL: NSURL(fileURLWithPath:      path!))
    config.readOnly = true
    
    // Open the Realm with the configuration
    let realm = try! Realm(configuration: config)
    

But this doesn't work , Please provide some solution to it .

Note : I don't want to migrate my database.

Thanks in advance

2 Answers

Select your realm file in Xcode Project Navigator then go open file inspector pane and check Target Membership box for your current target. Your code will work already!

Related