Junaid Khan
Junaid Khan

Reputation: 125

Issue in adding data in Realm in iOS

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?

Upvotes: 1

Views: 2429

Answers (2)

Pritam Hazra
Pritam Hazra

Reputation: 86

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.

Upvotes: 3

vsnv
vsnv

Reputation: 121

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)
        }
    }

Upvotes: 0

Related Questions