skip to Main Content

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


  1. 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

    var config = Realm.Configuration.defaultConfiguration
    config.schemaVersion = 5
    config.migrationBlock = { migration, oldSchemaVersion in }
    Realm.Configuration.defaultConfiguration = config
    

    MVVM Structure

    Create a Notification Model

    import UIKit
    import RealmSwift
    import ObjectMapper
    
    
    public class NotificationResults:Object,Mappable{
        @objc dynamic var status:Int = 0
        @objc dynamic var message:String = ""
        @objc dynamic var current_page:Int = 0
        @objc dynamic var per_page:Int = 0
        var data  : [String : Any] = [:]
        var notificationData = List<NotificationModel>()
        
        /// Primary Key
        public override static func primaryKey() -> String? {
            return "status" /* you can use like id,staus,message..etc..*/
        }
        /// Init Map
        required convenience public init?(map: Map) {
            self.init()
        }
        /// Mapping
        public func mapping(map: Map) {
            status <- map["status"]
            message <- map["message"]
            current_page <- map["data.current_page"]
            per_page <- map["data.per_page"]
            notificationData <- (map["data.data"], ArrayTransform<NotificationModel>())
            
        }
        
    }
    class NotificationModel: Object,Mappable {
       @objc dynamic var id:Int = 0
       @objc dynamic var title:String = ""
       @objc dynamic var message:String = ""
       @objc dynamic var datetime:String = ""
       /// Primary Key
       public override static func primaryKey() -> String? {
           return "id"
       }
       /// Init Map
       required convenience public init?(map: Map) {
           self.init()
       }
       /// Mapping
       public func mapping(map: Map) {
           id <- map["id"]
           title <- map["title"]
           message <- map["message"]
           datetime <- map["published_at"]
       }
    }
    
    Login or Signup to reply.
  2. The issue is that your Diary object does not have a primary key, so this will fail

    realm.add(diary, update: .modified)
    

    From the documentation

    If your model class includes a primary key, you can have Realm
    intelligently update or add objects based off of their primary key
    values using Realm().add(_:update:).

    To fix, add a primary key to your Diary object

    class Diary: Object {
       @objc dynamic var _id = UUID().uuidString
    
       override static func primaryKey() -> String? {
           return "_id"
       }
    }
    

    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

    To work with Realm Sync, your data model must have a primary key field
    called _id. _id can be of type string, int, or objectId.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search