将对象持久化到RealmSwift时出错对象类型''在Realm中不存在'

时间:2016-06-24 13:35:01

标签: swift realm

确定发现了问题!

我正在开始一个从Web获取成员的线程,并立即使用RxSwift将查询结果作为observable返回:

func getBoardMembersGroupedByType() -> Observable<[MemberType: [Member]]> {

    MeetingRest().getBoardMembers()
        .subscribeOn(OperationQueueScheduler(operationQueue: NSOperationQueue()))
        .subscribeNext { members in DataManager().updateOrInsertObjects(members) }
        .addDisposableTo(bag)

    return DataManager().getObjects(Member)
        .map{ members in members.groupedBy { member in member.memberTypeEnum }
    }

}

此行DataManager().updateOrInsertObjects(members)发生在此DataManager().getObjects(Member)之前,这就是原因。

我认为这是Realm的一个问题,因为它应该在插入之前检查并创建表格,这与查询时的方式相同。

原始问题

我正在尝试将以下对象保留到领域但是当我运行我的应用程序时,我收到错误对象类型'PhoneNumber'未在Realm'中保留。它出什么问题了?

  • 我正在使用默认的Realm配置。
  • XCode 7.3.1
  • Swift 2.2
  • 境界(0.103.1):
    • 领域/标题(= 0.103.1)
  • Realm / Headers(0.103.1)
  • RealmSwift(0.103.1):
    • 领域(= 0.103.1)

当我尝试从JSON字典创建对象时抛出异常。

var dict = // Load json object
var member = Member(value: dict)

这是Realm对象的代码定义:

class Entity: Object {
    dynamic var id: Int64 = 0
    override static func primaryKey() -> String? {
        return "id"
    }
}

class Member: Entity {
    dynamic var name: String!
    dynamic var email: String!
    dynamic var role: String!
    dynamic var curriculumUrl: String!
    dynamic var curriculumThumbnailUrl: String!
    dynamic var pictureUrl: String!
    private dynamic var memberType: String!

    let phoneNumbers = List<PhoneNumber>()

    var memberTypeEnum: MemberType {
        return MemberType(rawValue: memberType) ?? .Member
    }
}

enum MemberType: String {
    case Member
    case Secretary
}

class PhoneNumber: Object {
    dynamic var number: String!
    dynamic var phoneType: String!
}

以下是我要反序列化的示例JSON对象:

{
    "id": 834,
    "name": "Sallie Oneil",
    "email": "sallieoneil@zillan.com",
    "pictureUrl": "http://placehold.it/100x100",
    "phoneNumbers": [
      {
        "number": "(998) 477-2928",
        "phoneType": "Work"
      },
      {
        "number": "(898) 467-2201",
        "phoneType": "Mobile"
      },
      {
        "number": "(963) 412-3882",
        "phoneType": "Mobile"
      },
      {
        "number": "(924) 409-2270",
        "phoneType": "Mobile"
      }
    ],
    "role": "eu",
    "curriculumUrl": "http://placehold.it/100x200",
    "curriculumThumbnailUrl": "http://placehold.it/100x200",
    "memberType": "Member"
  }

0 个答案:

没有答案