NSfetchRequest 啥都不返回
Posted
技术标签:
【中文标题】NSfetchRequest 啥都不返回【英文标题】:NSfetchRequest return nothingNSfetchRequest 什么都不返回 【发布时间】:2019-07-08 11:52:58 【问题描述】:当从核心数据库中获取数据并同时更新同一实体的数据时,fetchRequest 返回 nil
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Abc")
fetchRequest.predicate = NSPredicate(format: "id = %@ ", productID)
do
let product = try mainContext.fetch(fetchRequest).first
return product as? Products
catch _ as NSError
//print("Could not fetch. \(error), \(error.userInfo)")
return nil
【问题讨论】:
【参考方案1】:实体与结果的转换类型不匹配
要么是
let fetchRequest = NSFetchRequest(entityName: "Abc")
...
do
let product = try mainContext.fetch(fetchRequest).first
return product as? Abc
或
let fetchRequest = NSFetchRequest(entityName: "Products")
do
let product = try mainContext.fetch(fetchRequest).first
return product as? Products
请以单数形式命名实体。从语义上讲,你投了product as? Product
【讨论】:
实体名称是 Products 而不是 ABC 然后按照第二个建议。以上是关于NSfetchRequest 啥都不返回的主要内容,如果未能解决你的问题,请参考以下文章
ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 啥都不做
NSFetchedResultsController 啥都不返回
当您啥都不返回时,从 collectionView(_:viewForSupplementaryElementOfKind:at:) 返回啥?