如何在xmppGroupCoreDataStorageObject中保存和获取?

时间:2016-12-08 11:17:57

标签: ios swift core-data xmppframework

使用xmpp-messenger-ios,我创建了组并设置了其配置并将用户添加到其中,然后我想将该组添加到xmppGroupCoreDataStorageObject存储中以将其列入OpenChatViewController表视图。

我如何将组数据保存到xmppGroupCoreDataStorageObject:

public class func addUserInCoreData(jid:String, users: Set<NSObject>) {

        let moc = OneRoster.sharedInstance.managedObjectContext_roster() as NSManagedObjectContext?
        let entity = NSEntityDescription.entityForName("XMPPGroupCoreDataStorageObject", inManagedObjectContext: moc!)
        let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: moc!)
        person.setValue(jid, forKey: "name")
        person.setValue(users, forKey: "users") // Code breaks here.
        print(moc.debugDescription)

        do{
            try moc?.save()
            let sucess = XMPPGroupCoreDataStorageObject.insertGroupName(jid, inManagedObjectContext: moc)
            print(sucess)
        }
        catch let error{
            print(error)
        }

    }

遇到异常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString managedObjectContext]: unrecognized selector sent to instance 0x7fdf8c545e50'***

以下是框架实施:

@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSSet* users;

2 个答案:

答案 0 :(得分:1)

好吧,您正在尝试将消息发送给无法响应的对象 查看您的异常日志:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString managedObjectContext]: unrecognized selector sent to instance 0x7fdf8c545e50'***  

managedObjectContext消息已发送到NSString类的实例 当然,它无法响应它而你正在崩溃。

答案 1 :(得分:0)

首先,因为你说它在users行的setValue上崩溃,所以有人猜测你的users集合实际上并不包含XMPPUserCoreDataStorageObject类型的对象(但它应该)。我认为你的方法签名应该是:

public class func addUserInCoreData(jid: String, users: Set<XMPPUserCoreDataStorageObject>)

检查调用此函数的位置,可能是您传递了一组非XMPPUserCoreDataStorageObject个对象,但有些不同的类型,这是错误的。

但可能更重要的是:

let entity = XMPPGroupCoreDataStorageObject.insertGroupName(jid, inManagedObjectContext: moc)

应该称为

  

让entity =   NSEntityDescription.entityForName( “XMPPGroupCoreDataStorageObject”   inManagedObjectContext:moc!)

try moc?.save()之后

。此方法返回应配置然后保存的XMPPGroupCoreDataStorageObject对象,而不是指示成功保存的bool。

退房,例如,本教程(请注意保存到核心数据部分中的saveName方法): https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial

祝你好运!