Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realm Swift detects lazy properties as optional, crashes when attempting to interpret them as RLMOptionalBase #3337

Closed
bdash opened this issue Mar 16, 2016 · 2 comments
Assignees
Milestone

Comments

@bdash
Copy link
Contributor

bdash commented Mar 16, 2016

A user on Stack Overflow has reported seeing a crash when attempting to save an object using code that looks like so:

class MyObject: Object {
    dynamic var someProperty: String = ""
    dynamic lazy var compoundPrimaryKey: String = self.compoundPrimaryKeyValue()
    private func compoundPrimaryKeyValue() -> String { return "SomeValue" }
    override static func primaryKey() -> String? { return "compoundPrimaryKey" }
}

try! Realm().write {
    Realm().add(MyObject())
}

This results in either a crash or an exception being thrown:

2016-03-15 18:07:57.988 TestHost[12404:378454] -[__NSTaggedDate underlyingValue]: unrecognized selector sent to instance 0x103aa3add

The crash and exception originate from RLMAddObjectToRealm:

* thread #1: tid = 0x5dec6, 0x00000001046658d6 Realm`RLMAddObjectToRealm(object=0x00006080000e6300, realm=0x000060000067c940, createOrUpdate=false) + 1830 at RLMObjectStore.mm:203, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x00000001046658d6 Realm`RLMAddObjectToRealm(object=0x00006080000e6300, realm=0x000060000067c940, createOrUpdate=false) + 1830 at RLMObjectStore.mm:203
    frame #1: 0x0000000104526eaa RealmSwift`Realm.add(object=0x00006080000e6300, update=false, self=0x0000608000028e60) -> () + 714 at Realm.swift:217
    frame #2: 0x00000001041f1c06 RealmSwift Tests`ObjectTests.(storeRealm=0x0000608000028e60) -> ()).(closure #1) + 54 at ObjectTests.swift:346
    frame #3: 0x0000000104529337 RealmSwift`thunk + 39 at Realm.swift:0
    frame #4: 0x0000000104777946 Realm`-[RLMRealm transactionWithBlock:error:](self=0x000060000067c940, _cmd="transactionWithBlock:error:", block=0x000060800004e880, outError=domain: nil - code: 140735238246560) + 86 at RLMRealm.mm:506
    frame #5: 0x0000000104526957 RealmSwift`Realm.write(block=0x00000001041f1bd0 RealmSwift Tests`RealmSwift_Tests.ObjectTests.(testFoo () -> ()).(closure #1) at ObjectTests.swift:345, self=0x0000608000028e60, $error=ErrorType @ 0x00007fff5fbfcbf0) -> ()) throws -> () + 215 at Realm.swift:111
    frame #6: 0x00000001041e3c0e RealmSwift Tests`ObjectTests.testFoo(self=0x0000608000267fc0) -> () + 462 at ObjectTests.swift:347

The property in question appears to be the internal storage of the lazy property, named compoundPrimaryKey.storage.

@quangpc-zz
Copy link

I meet the same issue here if use:
dynamic lazy var compoundPrimaryKey

@austinzheng
Copy link
Contributor

A fix should roll out in the next release. Swift lazy properties are explicitly forbidden, unless they are ignored. Ignored lazy properties should work properly now.

@jpsim jpsim mentioned this issue Jun 24, 2016
@bdash bdash modified the milestones: 1.0.1, 1.1.0 Jul 14, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants