I am making a diary app with Realm Swift and FSCalendar.
When I press the button to add to Realm, the app crashes and the title message is displayed.
Realm Swift is update 10.2.0.
I am testing it on an actual machine.
I deleted the application of the actual machine and installed it.
Do you know the solution?
Xcode12.2 mac11.0.1
import UIKit
import RealmSwift
class PrimaryData: Object {
@objc dynamic var id: Int = 0
@objc dynamic var Diary: String = ""
override static func primaryKey() -> String? {
return "Diary"
}
}
@IBAction func didTapAddNoteButton(_ sender: Any) {
let realm = try! Realm()
let diary = Diary()
diary.date = date
try! realm.write {
realm.add(diary, update: .modified)
}
self.performSegue(withIdentifier: "toSegueViewController", sender: nil)
}
import UIKit
import RealmSwift
class Diary: Object {
@objc dynamic var date: String = ""
@objc dynamic var context: String = ""
open var primaryKey: String {
return "date"
}
}
2
Answers
AppDelegate in Migration Code because app not delete when changes in model anythings.
Please update pod and re-install
pod ‘ObjectMapper’
pod ‘RealmSwift’
Migration Datababse if changes in Module add or remove Object than schemaVersion increment static
MVVM Structure
Create a Notification Model
The issue is that your Diary object does not have a primary key, so this will fail
From the documentation
To fix, add a primary key to your Diary object
Please note that if you’re ever going to sync using MongoDB Realm, then you must name your primary key
_id
. See the documention which states