快速从核心数据中获取数据时应用程序崩溃
Posted
技术标签:
【中文标题】快速从核心数据中获取数据时应用程序崩溃【英文标题】:App getting crashed while fetching data from core data swift 【发布时间】:2018-07-23 10:22:43 【问题描述】:这是获取数据的完整代码。 这是从核心数据中获取数据的代码。这里有什么问题?请帮助获取数据时出了什么问题
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
【问题讨论】:
什么是崩溃错误? @PrashantTukadiya 这是发生的崩溃消息 Fatal Exception: NSGenericException *** Collection <__nscfset:> was mutated while being enumerated. 如果您更改了任何实体,您需要重新安装应用程序。 你是否在***中检查过类似的问题,例如***.com/questions/38434129/… @PrashantTukadiya 我检查过那个帖子,但它没有帮助 【参考方案1】:我不确定,但您在获取数据时尝试更新或保存,或者您的集合类型可能是不可变的并且您正在尝试对其进行变异。
【讨论】:
以上是关于快速从核心数据中获取数据时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章