在iOS中的Realm中添加数据的问题 [英] Issue in adding data in Realm in iOS

查看:268
本文介绍了在iOS中的Realm中添加数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用领域的新手,我正在尝试将我的api响应保存在领域数据库中.为此,我阅读了文档并开始工作,我创建了Objects类,其中有我的变量,当我在领域应用程序中添加数据时发生错误时,我想在其中保存数据,错误为Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.这是我的对象类,

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

我的应用程序在此行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. 将所有变量设为@objc dynamic var.
  2. 确保您的realm对象在类中是全局的,否则请从self.realm.add(newListing)中删除self.
  3. 确保分配给newListing变量的所有值都不是nil(那些已经使用某些默认值初始化的变量).
  4. TCHChannelTCHMemberTCHMessage的实际数据类型是什么? Realm可能不支持这些类型.
  5. 确保在向领域添加条目后没有修改SingleChatRealm类结构.在这种情况下,您必须删除旧的.realm文件并创建一个新文件.
  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.

这篇关于在iOS中的Realm中添加数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆