XMPPFramework Swift获取对象始终为null

时间:2015-09-30 04:27:17

标签: swift xmppframework

嗨我使用swift的xmppframework有问题,我在获取对象时总是得到0个元素

func fetchedResultsController() -> NSFetchedResultsController? {
    if fetchedResultsControllerVar == nil {

        let moc = managedObjectContext_roster() as NSManagedObjectContext?
        let entity = NSEntityDescription.entityForName("XMPPUserCoreDataStorageObject", inManagedObjectContext: moc!)

        let sd1 = NSSortDescriptor(key: "sectionNum", ascending: true)
        let sd2 = NSSortDescriptor(key: "displayName", ascending: true)

        let sortDescriptors = [sd1, sd2]
        let fetchRequest = NSFetchRequest()

        fetchRequest.entity = entity
        fetchRequest.sortDescriptors = sortDescriptors
        fetchRequest.fetchBatchSize = 10

        fetchedResultsControllerVar = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc!, sectionNameKeyPath: "sectionNum", cacheName: nil)
        fetchedResultsControllerVar?.delegate = self

        do {
            try fetchedResultsControllerVar!.performFetch()

        } catch let error as NSError {
            print("Error: \(error.localizedDescription)")
            abort()
        }
        //  if fetchedResultsControllerVar?.performFetch() == nil {
        //Handle fetch error
        //}
    }

    return fetchedResultsControllerVar!
}

我已使用此代码在我的设置中激活

xmppStreams = externalStream

    xmppRosterStorage = XMPPRosterCoreDataStorage()
    xmppRoster = XMPPRoster(rosterStorage: xmppRosterStorage)

    xmppRoster!.autoFetchRoster = true;
    xmppRoster!.autoAcceptKnownPresenceSubscriptionRequests = true;

    xmppRoster!.addDelegate(self, delegateQueue: dispatch_get_main_queue())
    xmppRoster!.activate(xmppStreams)

但是当我在委托函数printctResultControllerVar.fetchedObjects中打印时,它总是返回0,但是对于jidList,我可以获得我的列表。

func xmppRosterDidEndPopulating(sender: XMPPRoster?){
    var jidList = xmppRosterStorage!.jidsForXMPPStream(xmppStreams)
    fetchedResultsControllerVar!.fetchedObjects

    print("List=\(jidList)")

}

我错过了什么吗?我必须在我的班级中初始化XMPPUserCoreDataStorageObject吗?感谢

1 个答案:

答案 0 :(得分:0)

即使您使用xmpp,也必须确保在获取roster之前已与XMPPCoreData保持联系。

我围绕Swift Wrapper制作了XMPPFramework,它将简化基本聊天应用的开发,并会向您展示如何正确获取roster

您可以查看Maven Antrun Plugin