Reputation: 125
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
Reputation: 86
There may be several reasons for that,
@objc dynamic var
.realm
object is global within the class, otherwise remove self
from self.realm.add(newListing)
.newListing
are not nil
(Those variables which you have already initiated with some default values).TCHChannel
, TCHMember
and TCHMessage
? These type may not be supported by Realm
.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
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