1

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?

Junaid Khan
  • 125
  • 2
  • 13
  • Those vars should be @objc dynamic var name =“” – Jay Mar 05 '19 at 15:53
  • when i set my variables with objc dynamic var it shows me error, Terminating app due to uncaught exception 'RLMException', reason: 'Property 'twChannelObj' is declared as 'TCHChannel', which is not a supported RLMObject property type. All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or subclasses of RLMObject. . @Jay – Junaid Khan Mar 05 '19 at 15:58
  • That's correct. Realm supports the types of objects listed and cannot manage other types of objects. See the Realm documentation [Supported Property Types](https://realm.io/docs/swift/latest/#supported-types) – Jay Mar 05 '19 at 18:16

2 Answers2

3

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.
  • First of all , i'm not modifying realm before adding data in it, second it is globally in class and these TCHChannel, TCHMember and TCHMessage data types are classes of twilio third party lib with base class of NSObject . The issue is coming in these class data types how this can be fixed? @Pritam Hazra – Junaid Khan Mar 06 '19 at 11:21
  • Realm supports boolean , byte , short , int , long , float , double , String , Date and byte[] field types. NSObject reference can't be saved on realm. Try to save specific data from TCHChannel, TCHMember and TCHMessage in any of the given formats. I mean not storing the hole object just try to store specific properties. @JunaidKhan – Pritam Hazra Mar 06 '19 at 12:56
  • but then i want then in an array of TCHMember later to show data in table view. @Pritam Hazra – Junaid Khan Mar 06 '19 at 13:39
  • See this link https://media.twiliocdn.com/sdk/ios/chat/releases/1.0.4/docs/Classes/TCHMember.html#//api/name/subscribedUserWithCompletion:. TCHMember consists this parameters. Just save these parameters separately. And for showing make TCHMember objects fetching the parameters from realm. – Pritam Hazra Mar 06 '19 at 16:42
  • I have stored the TCHMember and TCHMessage parameter in realm and now i should create class of realm Object for TCHMember and get those parameters in that ? @Pritam Hazra – Junaid Khan Mar 06 '19 at 17:37
0

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)
        }
    }
vsnv
  • 111
  • 3
  • I have run through your code my app is crashing with this error, Terminating app due to uncaught exception 'RLMException', reason: 'Table has no columns. @vsnv – Junaid Khan Mar 05 '19 at 15:04
  • You can see the answer here: https://github.com/realm/realm-cocoa/issues/4169#issuecomment-250918095. You should mark persisted properties in realm object with 'dynamic' keyword. – vsnv Mar 05 '19 at 15:40
  • when i use dynamic it shows me error 'dynamic' var 'twChannelObj' must also be '@objc' . @vsnv – Junaid Khan Mar 06 '19 at 11:27