Raymond Moay
Raymond Moay

Reputation: 321

Realm setting custom fileURL confusion

I'm new to realm, and so as I was fooling around with it to learn it, I found something quite interesting. In my appDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.Hobo.RealmDatabase")!
    var config = Realm.Configuration()
    config.fileURL = directory.filePathURL?.URLByAppendingPathComponent("db.realm")
    Realm.Configuration.defaultConfiguration = config
    let realm = try! Realm()
    print("File Location: \(realm.configuration.fileURL!)") // -> Location A
    print("NO OF USERS: \(realm.objects(User).count)") // -> 0
    return true
}

but in my ViewController:

 let realm = try! Realm()

 override func viewDidLoad() {
    super.viewDidLoad()

    print("NO OF USERS IN VIEWDIDLOAD: \(realm.objects(User).count)") // -> 1

    let firstTime = loadFirstTime()
    if firstTime {
        // configure USER!
        let user = User()
        user.monthlyIncome = 50000
        try! realm.write({
            realm.add(user)
        })
        saveFirstTime(false)
        print("First time, user written")
    }
    dailyLimit.text = String(realm.objects(User).first!.dailyLimit)

}

Notice the returns from the print() functions. In app delegate, the result of the print(number of users:) returns 0, but in the viewController's viewDidLoad, it returned a 1.

Isn't both supposed to return the same value? In this case 1?

Thanks in advance!!

Upvotes: 0

Views: 2583

Answers (2)

dimohamdy
dimohamdy

Reputation: 3053

depend on documentation of realm 3

https://realm.io/docs/swift/latest/#realm-configuration

func setDefaultRealmForUser(username: String) {
        var config = Realm.Configuration()

        // Use the default directory, but replace the filename with the username
        config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("\(username).realm")

        // Set this as the configuration used for the default Realm
        Realm.Configuration.defaultConfiguration = config
    }

Upvotes: 0

MCMatan
MCMatan

Reputation: 8863

Yes it is the same, I'm guessing you removing by mistake the user, on application load, or something like that, you should use "Realm browser" to check your DB state, that way you can see when an object changes during run time. https://github.com/realm/realm-browser-osx

EDIT

Check your accessing the default configuration. In realm you can have multiple configurations like so:

let config = Realm.Configuration(
    // Get the URL to the bundled file
    fileURL: NSBundle.mainBundle().URLForResource("MyBundledData", withExtension: "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)

Upvotes: 1

Related Questions