主键未在模型中定义,但显示主键错误

时间:2017-03-27 01:13:40

标签: swift realm realm-browser

我收到此错误...

  

2017-03-26 17:34:49.104919手机[518:254067] ***
  由于未捕获的异常终止应用程序' RLMException',
  原因:'只有'字符串'和' int'属性可以指定为主键'

在预先填充的领域中,我有所有的字符串列,除了2个双列用于lat,lng。

这是我的模特:

import Foundation
import RealmSwift

class Destination: Object{

dynamic var destinationSlackChannelName = ""
dynamic var destinationSlackChannelId = ""
dynamic var destinationName = ""
dynamic var destinationType = ""
dynamic var destinationCode = ""
dynamic var destinationRegionCode = ""
dynamic var destinationSiteSlackChannelName = ""
dynamic var destinationCity = ""
dynamic var destinationCountry = ""
dynamic var destinationStatus = ""
dynamic var destinationLastUpdated = ""
dynamic var lat:Double = 0.0
dynamic var lng:Double = 0.0

}

以下是我在名为RealmManager的单例中配置和查询域的方法....

    func getHebronDestinations() -> Results<Destination> {
        let bundleUrl = Bundle.main.url(forResource: "default", withExtension: "realm")
    let config = Realm.Configuration(
        fileURL: bundleUrl,
        readOnly: true,
        schemaVersion: 0,
        migrationBlock: { migration, oldSchemaVersion in
            if (oldSchemaVersion < 1) {
                Log.info?.message("\(oldSchemaVersion)")
                Log.info?.message("wtf")
            }
            if (oldSchemaVersion < 2) {
                Log.info?.message("\(oldSchemaVersion)")
            }
            Log.info?.message("Realm migration did run")  // Log to know migration was executed
        }
    )

    let realm = try! Realm(configuration: config)
    let naoHebronResults = realm
        .objects(Destination.self)
        //.filter("destinationRegionCode == 'nao' AND destinationCode == 'heb'")
    Log.info?.message("\(naoHebronResults)")
    for res in naoHebronResults{
        Log.info?.message(res.destinationName)
    }
    return naoHebronResults
}

这是ViewController中的函数调用...

override func viewDidLoad() {
    super.viewDidLoad()
    print("viewDidLoad")
    let realmManager = RealmManager.shared
    let hebDevices = realmManager.getHebronDestinations()
    print(hebDevices)
}

如果模型中没有主键,为什么还会出现主键错误?

我通过领域浏览器从csv创建了这个领域文件......之前它工作过一次。

1 个答案:

答案 0 :(得分:1)

如果您在“正在播放”的模型上没有主键,则无关紧要。我的模型不在预先填充的领域内,而且这些模型上有主键。

我认为只有“在游戏中/境内”的模特才算数。

任何带有主键的Object子类都会导致错误... Terminating app due to uncaught exception 'RLMException', reason: 'Only 'string' and 'int' properties can be designated the primary key'

感谢https://github.com/bdash 和他的push

  

您可以在项目中搜索名为primaryKey to的函数吗?   看你是否忽略了一个?

以及https://stackoverflow.com/users/3736093/fahim