0

I'm getting this error...

2017-03-26 17:34:49.104919 Mobile[518:254067] ***
Terminating app due to uncaught exception 'RLMException',
reason: 'Only 'string' and 'int' properties can be designated the primary key'

In the prepopulated realm I have all string columns, except for 2 Double columns for lat,lng.

Here's my model:

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

}

Here's how I'm configuring and querying realm in a singleton called 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
}

And here's the function call in a ViewController...

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

Why am I still getting that primary key error, if there is no primary key in the model?

I made this realm file from a csv via realm browser...it worked once before.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sirvon
  • 2,547
  • 1
  • 31
  • 55
  • I have not used Realm in a long time so this might be something that Realm catches but is it possible that your prepopulated Realm has a primary key set when your model does not? Are you able to browse your prepoulated Realm to see if it has a primary key set? – Fahim Mar 27 '17 at 01:44
  • yeah i can browse it throught realm-browser... – sirvon Mar 27 '17 at 01:50
  • But I dont see anything signifying a primary key..All columns in realm file are strings...i took out the Doubles...and still same error – sirvon Mar 27 '17 at 01:50
  • I don't believe it's the Double values which are causing the issue. According to the Realm code, you get that error when a primary key is defined and it is not a String or an Int. So Realm does think that a primary key is defined - the question is why? Are you by any chance able to share the project with me? If yes, I can take a look ... – Fahim Mar 27 '17 at 01:52
  • my dependencies are all over the place...i have to get the project in a diff state to share...im going to make a new project with same realm file, model and manager and see what happens.. – sirvon Mar 27 '17 at 02:03
  • That's actually a great approach :) Simplifying might simply show you what is going on so that you can fix it yourself. If not, just share the simplified project with me and I'll take a look. – Fahim Mar 27 '17 at 02:09
  • @fahim https://s3-ap-southeast-1.amazonaws.com/dilini/moreCHOPS.zip – sirvon Mar 27 '17 at 03:21
  • it worked in a clean project...there must be something going on with cache in previous project....i clear derived data – sirvon Mar 27 '17 at 03:24
  • 1
    The things to try in that situation would be clean project, delete app and re-install, delete derived data, and quit and re-launch Xcode. One of those should hopefully help :) – Fahim Mar 27 '17 at 03:26
  • btw @fahim what do you use instead of Realm? – sirvon Mar 27 '17 at 06:53
  • 1
    I use [SQLiteDB](https://github.com/FahimF/SQLiteDB) a lightweight SQLite wrapper that I wrote myself. It isn't as snazzy as Realm but I find that it works better for my own needs :) – Fahim Mar 27 '17 at 07:15

1 Answers1

1

It doesn't matter if you don't have a Primary Key on models that are "in play". I had models that weren't inside the pre populated realm yet, and THOSE models had Primary Keys on them.

I thought only models "in play/inside the realm" counted.

Any Object subclasses with a Primary Key will cause the error... Terminating app due to uncaught exception 'RLMException', reason: 'Only 'string' and 'int' properties can be designated the primary key'

thanks to https://github.com/bdash and his push

Can you do a search in your project for a function named primaryKey to see if you've overlooked one?

and also https://stackoverflow.com/users/3736093/fahim

Community
  • 1
  • 1
sirvon
  • 2,547
  • 1
  • 31
  • 55