解析Realm模型时应用程序崩溃或冻结

时间:2015-02-06 11:30:21

标签: ios iphone swift realm

我有这个Realm模型定义:

class PSTChatThread: RLMObject {
    dynamic var channelName = ""
    dynamic var environment = RLMObject(object: PSTChannelEnv.className())
    dynamic var chatMessages = RLMArray(objectClassName: PSTMessage.className())
}

class PSTChannelEnv: RLMObject {
    dynamic var associatedPlaceId = 0
    dynamic var chattingWithUuid = ""
    dynamic var chattingWithUsername = ""
    dynamic var hasSessionEnded = false
    dynamic var unreadMessages = 0
}

class PSTMessage: RLMObject {

    // type
    dynamic var typeRaw = ""
    var type: PSTProtocolMsgTypes {
        get {
            if let a = PSTProtocolMsgTypes(rawValue: typeRaw) {
               return a
           }
           return .ChatMsg
        }
     }

    // envelope
    dynamic var channel = ""
    dynamic var timeoken = 0

 }

class ChatMsg: PSTMessage {
    dynamic var msgId = ""
    dynamic var text = ""
    dynamic var username = ""
    dynamic var uuid = ""
    dynamic var associatedPlaceId = -1
 }

 class ACKMsg: PSTMessage {
    dynamic var forId = ""
    dynamic var uuid = ""
 }

 class UserTypingMsg: PSTMessage {
    dynamic var uuid = ""
 }

 class UserStoppedTypingMsg: PSTMessage {
    dynamic var uuid = ""
 }

 class SessionUpdateMsg: PSTMessage {
     dynamic var hasSessionEnded = false
     dynamic var uuid = ""
 }

 class ContactDetailsMsg: PSTMessage {
    dynamic var accessLevel = -1
    dynamic var uuid = ""
 } 

 class ReadReceiptMsg: PSTMessage {
    dynamic var uuid = ""
 }
  • 当我运行应用时,它会崩溃:prop.column = table.add_column_link(tightdb::DataType(prop.type), prop.name.UTF8String, *linkTable); EXC_BAD_ACCESS
  • 假设模型从上到下进行解析,如果我将PSTChatThread移到底部,应用程序就会冻结。没有崩溃或任何事情,只是变得没有反应。

PSTChatThread定义出现了一些问题。如果这被注释掉,一切似乎都能正常工作。

每当我对模型进行RMRealm调用或任何其他查询调用时,都会发生这种情况。

如果是我的子类逻辑,我想再问一件事。有没有办法PSTMessage没有被持久化到模式但只有它的子类呢?

谢谢

1 个答案:

答案 0 :(得分:1)

问题在于这一行

dynamic var environment = RLMObject(object: PSTChannelEnv.className())

只需要

dynamic var environment: PSTChannelEnv

我们拥有objectClassName的原因是因为您正在创建一个类型为objectClassName的数组。在进行一对一关系时,您已经知道对象类,因此可以直接设置类型。

以下是setting up your models in Realm

上的更多文档

当你尝试做这样的事情时,我们应该抛出一个错误。谢谢你指出来了!