Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-313

Returning optional of generic type parameter constrained by `Object` causes crash

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      Goals

      <!--- What do you want to achieve? -->
      Trying to get an object from my Realm database

      Expected Results

      <!--- What did you expect to happen? -->
      Successfully get the required object

      Actual Results

      <!--- What happened instead?
      e.g. the stack trace of a crash
      -->
      I get a crash with an uncaught exception:

      "Inside init: Current thread <NSThread: 0x600000075700>{number = 5, name = (null)}"
      "Inside getObject: Current thread <NSThread: 0x600000075700>{number = 5, name = (null)}"
      libc++abi.dylib: terminating with uncaught exception of type NSException
      

      At first I thought it was a threading issue, but you can see that I init the Realm and getObject on the same thread.

      Code Sample

      <!---
      Provide a code sample or test case that highlights the issue.
      If relevant, include your model definitions.
      For larger code samples, links to external gists/repositories are preferred.
      Alternatively share confidentially via mail to help@realm.io.
      Full Xcode projects that we can compile ourselves are ideal!
      -->

      private var database: Realm!
      
      init(some_var: String) {
        var configuration = Realm.Configuration()
        configuration.fileURL = configuration.fileURL!.deletingLastPathComponent().appendingPathComponent("\(some_var).realm")
        do {
          debugPrint("Inside init: Current thread \(Thread.current)")
          self.database = try Realm(configuration: configuration)
        } catch {
          debugPrint("realmInit: Can't create realm database.")
        }
      }
      
      func getObject<T: Object, K>(with primaryKey: K) -> T? {
        debugPrint("Inside getObject: Current thread \(Thread.current)")
        return self.database.object(ofType: T.self, forPrimaryKey: primaryKey) // THIS LINE THROWS EXCEPTION
      }
      

      Version of Realm and Tooling

      <!---
      In the CONTRIBUTING guidelines, you will find a script,
      which will help determining some of these versions.
      -->
      Realm framework version: 2.6.2

      Realm Object Server version: N/A not using

      Xcode version: 8.3.2

      iOS/OSX version: OSX 10.12.3

      Dependency manager + version: Cocoapods 1.1.1

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: