Issue in adding data in Realm in iOS

别来无恙 提交于 2019-12-13 01:09:18

问题


i'm new in using realm, i'm trying to save my api response in realm database. For that i read out there documents and started my work, I have created class of Objects in which a have my variables in which i want to save data now when i add data in realm app crashes with error, Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. This is my class of Objects,

class SingleChatRealm: Object {

var actualNameFor_1_2_1_chat = ""
var isGroup : Bool  = true
var isNewGroup : Bool = false
var lastMessage = ""
var lastMsgRead : Bool = false
var lastMsgTime = ""
var lastMsgTimeActual = ""
var name = ""
var profilePic = ""
var roomSID = ""
var unReadMsgsCount = 0
var twChannelObj : TCHChannel?
var members = [TCHMember]()
var messages = [TCHMessage]()
// @objc dynamic var group_info : [String:JSON]?

} and this is how i'm storing data in realm,

 let realm = try! Realm()

        try! realm.write {

            let newListing = SingleChatRealm()

            for items in dateWiseSortedSingleRooms
            {
                newListing.actualNameFor_1_2_1_chat = items.actualNameFor_1_2_1_chat
                newListing.isGroup = items.isGroup
                newListing.isNewGroup = items.isNewGroup
                newListing.lastMessage = items.lastMessage
                newListing.lastMsgRead = items.lastMsgRead
                newListing.lastMsgTime = items.lastMsgTime
                newListing.lastMsgTimeActual = items.lastMsgTimeActual
                newListing.members = items.members
                newListing.messages = items.messages
                newListing.name = items.name
                newListing.profilePic = items.profilePic!
                newListing.roomSID = items.roomSID
                newListing.twChannelObj = items.twChannelObj
                newListing.unReadMsgsCount = items.unReadMsgsCount
                print(newListing)
                self.realm.add(newListing)
            }
        }

My app crashes on this line self.realm.add(newListing) with above given error, why is it so? what's i'm missing in this?


回答1:


There may be several reasons for that,

  1. Make all the variables @objc dynamic var.
  2. Make sure your realm object is global within the class, otherwise remove self from self.realm.add(newListing).
  3. Make sure all the values you are assigning to the variables of newListing are not nil(Those variables which you have already initiated with some default values).
  4. What are the actual data type of TCHChannel, TCHMember and TCHMessage? These type may not be supported by Realm.
  5. Make sure you did not modify the SingleChatRealm class structure after adding an entry to realm. In that case you have to delete the old .realm file and have to create a new one.



回答2:


You just created a new instance of Realm, but self.realm is still nil, you should add line:

self.realm = realm

to your code:

let realm = try! Realm()

self.realm = realm

    try! realm.write {

        let newListing = SingleChatRealm()

        for items in dateWiseSortedSingleRooms
        {
            newListing.actualNameFor_1_2_1_chat = items.actualNameFor_1_2_1_chat
            newListing.isGroup = items.isGroup
            newListing.isNewGroup = items.isNewGroup
            newListing.lastMessage = items.lastMessage
            newListing.lastMsgRead = items.lastMsgRead
            newListing.lastMsgTime = items.lastMsgTime
            newListing.lastMsgTimeActual = items.lastMsgTimeActual
            newListing.members = items.members
            newListing.messages = items.messages
            newListing.name = items.name
            newListing.profilePic = items.profilePic!
            newListing.roomSID = items.roomSID
            newListing.twChannelObj = items.twChannelObj
            newListing.unReadMsgsCount = items.unReadMsgsCount
            print(newListing)
            self.realm.add(newListing)
        }
    }


来源:https://stackoverflow.com/questions/55005270/issue-in-adding-data-in-realm-in-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!