0

Last Friday, it worked. Today, it doesn't work anymore.

I have a colleague for whom it's working fine.

I have reinstalled my pods, cleaned the project, tried different targets. I have no error messages.

Here is the faulty code: Faulty code

This is confusing to me. Any idea what I should be looking for ?

Adrien
  • 2,088
  • 1
  • 18
  • 35
  • Obviously `Realm()` throws an error, but since you're using `try!` instead of using Do-Catch and error handling, you're left with just a crash instead of an error message. – Eric Aya Apr 04 '16 at 10:01
  • Have you tried deleting the app from the simulator? – bcamur Apr 04 '16 at 10:19
  • @EricD Thanks for noticing. But how can I declare an app wise variable with a do/catch statement as it is not allowed at the top level? – Adrien Apr 04 '16 at 12:22
  • @bcamur Thanks though it didn't change anything. – Adrien Apr 04 '16 at 12:23
  • 1
    Is any information logged to the console? You can look by selecting View -> Debug Area -> Activate Console within Xcode. – bdash Apr 04 '16 at 18:59

1 Answers1

0

Note: I don't work with Realm, so I could be off-topic here... Don't hesitate to tell me.

We see that Realm() throws an error, but since you're using try! instead of using Do-Catch and error handling, you're left with just a crash instead of an error message.

Since you're asking in a comment "But how can I declare an app wise variable with a do/catch statement as it is not allowed at the top level?", I would just make the property an Optional and populate its content when the view/window/init loads.

At the root of the class:

let realm: Realm?

In viewDidLoad, or init, or any other method called at startup that will fit your needs:

do {
    self.realm = try Realm()
} catch let error as NSError {
    print(error.debugDescription)
    // handle errors here
}

Then in your app, safely unwrap before usage:

if let realm = self.realm {
    // do your work with `realm`
}

or even better, follow the "happy path" and use guard:

guard let realm = self.realm else { return }
// do your work with `realm`

If you don't want to have to handle unwrapping the Optional property everywhere, you can declare it as an implicitly unwrapped optional instead:

let realm: Realm!

But then in the init part you must not forget to stop the app execution if it's nil, before trying to access the value.

do {
    self.realm = try Realm()
} catch let error as NSError {
    print(error.debugDescription)
    fatalError("Can't continue further, no Realm available")
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • So there is no way to have a generic constant/variable I can access from anywhere? So far, I used to declare a variable before `AppDelegate` and access it from anywhere. Like so: https://gist.github.com/AdrienGiboire/ab874b1faead0089a38ca608f5f0ee63 which is not possible in what you propose me to do. Am I missing something? – Adrien Apr 05 '16 at 08:03
  • 1
    Well, your way of doing it is considered a bad pattern because global variables like this is can cause problems. If you really - and I mean *really* - need to work with something global, at least embed it in a singleton like this: http://stackoverflow.com/a/36012158/2227743 Then you can access the singleton's sharedInstance from anywhere. – Eric Aya Apr 05 '16 at 08:08
  • Thanks. That's precious advice. I'm new to iOS development :) So you suggest I declare the variables every time I need them even if it's everywhere? – Adrien Apr 05 '16 at 08:19
  • 1
    No, the key is "dependency injection". When you need to use `realm` outside of the class you created it in, you *send it* to the object that needs it. Like, you init another class with a `realm: Realm` parameter and you pass the existing instance. Or you just pass the realm instance to a method, etc. // You *can* be global, with a singleton, but it's not ideal at all. It's attractive because easy but is a pain to test, and is dangerous if the app uses multiple threads (like: what happens if two threads modify the value of a property in the singleton *at the same time*...). – Eric Aya Apr 05 '16 at 08:22
  • 1
    "Dependency injection" is just the safe way of doing this. :) Good explanations [here](https://www.objc.io/issues/13-architecture/singletons/) (for Objective-C but the reasoning still applies) and excellent blog article [here](http://bartjacobs.com/dependency-injection-in-swift/) specifically about Swift. – Eric Aya Apr 05 '16 at 08:48