Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Intermediate Realm on iOS Part 1: Bundled Realm Files

In this Realm video tutorial, you'll learn how to include a file pre-populated with data in your app.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3245-intermediate-realm-on-ios/lessons/2

Hi @icanzilb, thanks for the tutorial. I have one question for this part:

dispatch_once(&configToken) { Exams.copyInitialData(NSBundle.mainBundle().URLForResource("default_v1.0", withExtension: "realm")!, to: RealmConfig.mainConfig.fileURL!) }

what do you think about forced unwrapping here? Is it safe enough for production apps? Or it should be wrapped with “if let” for safety?

1 Like

Hi silberiaman, you can definitely add an if let in your code if that will feel safer. I personally use force unwrapping everywhere where having a nil value would never make sense - so that any potential errors can be caught during development and testing.

Hi @icanzilbI, I tried implementing the following code, and the associated code, into my project:

struct Exams {
  static func copyInitialData(from: NSURL, to fileURL: NSURL) {
    if !fileURL.checkPromisedItemIsReachableAndReturnError(nil) {
      _ = try? NSFileManager.defaultManager().removeItemAtURL(fileURL)
      try! NSFileManager.defaultManager().copyItemAtURL(from, toURL: fileURL)
    }
  }
}

However, I received the following errors: 1) use of undeclared type NSURL and 2) use of unresolved identifier NSFileManager. Could you provide insight into why this issue might be happening? Many thanks in advance.

Hi @icanzilb, great tutorial! However, I am new to Realm and it is unclear to me how to actually create a pre bundled .realm file and upload data to it. How are these two things accomplished?

hi @alexcowley - in the same way, explain in this video series - you create an app, define the objects you want to store in your file, and then create them. You can then grab the ready file and bundle it up with your app!

@icanzilb Thank you for the response. I have a Parse database that I query multiple Collections, and I currently have a method that runs all the queries at once (when the user logs in) is it possible to put data from different Collections in the same .realm file? If so how would I go about extracting the data from my method and saving it to the file?

I’ve never used Parse so I’m not really the person to ask, but I’m sure you can save anything you want into a realm file :slight_smile:

Hi-

I have the same question. Are these the basic steps:

  1. Have app build realm programatically
  2. Locate directory on disk of app and realm file.
  3. Copy that realm file to your project

Is it that simple?

Also, with the new Realm there is an object browser that talks to their server. Is there a similar tool at least for viewing Realm files?

Thanks,
Bruce

P.S. - I had this same problem with the Core Data tutorials a few years ago. You guys really need some guidance on setting up the static file. You are assuming a lot.

The Realm Browser opens both local and remote files. The steps you’re describing are correct, use an app to create the file and pre-populate data, then just include that file in your app’s bundle.

In case anyone is following along in swift 3. And run’s into the issue of dispatch_once_t being deprecated below is a working version of the realmConfig enum and one version on how to execute code one time:

enum RealmConfig {
    private static let mainConfig = Realm.Configuration(fileURL: URL.inDocumentsFolder("main.realm"),
                                                        schemaVersion: 1)
    case Main
    
    var configuration: Realm.Configuration {
        switch self {
        case .Main:
            let _ : () = {
                Exams.copyInitialData(Bundle.main.url(forResource: "default_v1.0", withExtension: "realm")!, to: RealmConfig.mainConfig.fileURL!)
            }()
            return RealmConfig.mainConfig
        }
    }

P.S. Feel free to comment/critique if my approach is incorrect or have better solutions. Thanks!