从核心数据快速获取数据时,应用崩溃

时间:2018-07-23 10:22:43

标签: ios swift core-data

这是获取数据的完整代码。 这是从核心数据中获取数据的代码。这里有什么问题?请帮助获取数据时出现问题

static func getChatMessagesForGroup( groupId : Double) -> [GroupConversationMessage]{
    AppDelegate.getAppDelegate().log.debug("\(groupId)")
    let managedContext = CoreDataHelper.getNSMangedObjectContext()

    var messages = [GroupConversationMessage]()
    var conversationObjects = [NSManagedObject]()
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: GROUP_CONVERSATION_TABLE_NAME)
    let predicate = NSPredicate(format: "\(GroupConversationMessage.FLD_GROUP_ID) = %@", argumentArray: [StringUtils.getStringFromDouble(decimalNumber : groupId)])
    fetchRequest.predicate = predicate
    let sortDescriptor = NSSortDescriptor(key: GroupConversationMessage.FLD_TIME, ascending: true)
    var sortDescriptors = [NSSortDescriptor]()
    sortDescriptors.append(sortDescriptor)
    fetchRequest.sortDescriptors =  sortDescriptors
    DispatchQueue.main.sync {
        do {
            let results = try managedContext.fetch(fetchRequest)
            conversationObjects = results as! [NSManagedObject]
        } catch let error as NSError {
            AppDelegate.getAppDelegate().log.error("Could not fetch \(error), \(error.userInfo)")
        }
    }

   for i in 0 ..< conversationObjects.count  {
        let message = GroupConversationMessage()
        message.uniqueID = conversationObjects[i].value(forKey: QuickRideMessageEntity.UNIQUE_ID) as? String
        message.id = conversationObjects[i].value(forKey: GroupConversationMessage.FLD_ID) as! Double
        message.groupId = conversationObjects[i].value(forKey: GroupConversationMessage.FLD_GROUP_ID) as! Double
        message.senderId = conversationObjects[i].value(forKey: GroupConversationMessage.FLD_SENDER_ID) as! Double
        message.senderName = conversationObjects[i].value(forKey: GroupConversationMessage.FLD_SENDER_NAME) as? String
        message.message = (conversationObjects[i].value(forKey: GroupConversationMessage.FLD_MESSAGE) as! String)
        message.time = conversationObjects[i].value(forKey: GroupConversationMessage.FLD_TIME) as! Double
        messages.append(message)
    }
    return messages
}

1 个答案:

答案 0 :(得分:0)

我不确定,但是您正在尝试在获取数据时进行更新或保存,或者您的集合类型可能是不可变的,并且您正在尝试对其进行突变。