Ios 核心数据中无法识别的选择器崩溃,除非我先调用saveContext
以下是我的代码中的三个函数:Ios 核心数据中无法识别的选择器崩溃,除非我先调用saveContext,ios,swift,core-data,Ios,Swift,Core Data,以下是我的代码中的三个函数: func workOutResults() { var fixtures = [Fixture]() let fixtureFetch: NSFetchRequest<Fixture> = Fixture.fetchRequest() do { fixtures = try coreDataStack.managedContext.fetch(fixtureFetch) } catch let error as NSError
func workOutResults() {
var fixtures = [Fixture]()
let fixtureFetch: NSFetchRequest<Fixture> = Fixture.fetchRequest()
do {
fixtures = try coreDataStack.managedContext.fetch(fixtureFetch)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
for fixture in fixtures {
// do stuff here
}
}
func processUpdates() {
// relevant code snippet below
let theTable = loadLeagueTableFor(leagueName: "Championship", cds: cds)
}
func loadLeagueTableFor(leagueName: String, cds: CoreDataStack) -> [Club] {
var leagueArray = [Club]()
// Set up the sort descriptors
let pointsSortDescriptor: NSSortDescriptor = {
let compareSelector = #selector(NSString.localizedStandardCompare(_:))
return NSSortDescriptor(key: #keyPath(Club.leaguePoints),
ascending: false,
selector: compareSelector)
}()
let goalDiffSortDescriptor: NSSortDescriptor = {
let compareSelector = #selector(NSString.localizedStandardCompare(_:))
return NSSortDescriptor(key: #keyPath(Club.leagueGoalDiff),
ascending: false,
selector: compareSelector)
}()
let goalsForSortDescriptor: NSSortDescriptor = {
let compareSelector = #selector(NSString.localizedStandardCompare(_:))
return NSSortDescriptor(key: #keyPath(Club.leagueGoalsFor),
ascending: false,
selector: compareSelector)
}()
let clubNameSortDescriptor: NSSortDescriptor = {
let compareSelector = #selector(NSString.localizedStandardCompare(_:))
return NSSortDescriptor(key: #keyPath(Club.name),
ascending: true,
selector: compareSelector)
}()
// Do the Fetch request of Clubs, placing them in order into leagueArray
let clubFetch: NSFetchRequest<Club> = Club.fetchRequest()
clubFetch.predicate = NSPredicate(format: "%K == %@", #keyPath(Club.league.nameID), leagueName)
clubFetch.sortDescriptors = [pointsSortDescriptor, goalDiffSortDescriptor, goalsForSortDescriptor, clubNameSortDescriptor]
do {
leagueArray = try cds.managedContext.fetch(clubFetch)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
cds.saveContext()
return leagueArray
}
。。。一切正常。但是如果我打电话
workOutResults()
coreDataStack.saveContext()
processUpdates()
workOutResults()
processUpdates()
。。。loadLeagueTableFor()中出现以下错误:
因此,在两次抓取之间保存上下文可以避免崩溃,但会带来一个问题,因为这意味着我必须在一个我不愿意保存的地方保存。知道我为什么会出现错误吗?不清楚为什么会出现这种情况,因为您正在将数值与
compareSelector
进行比较,您将其定义为NSString
上的一种方法。错误消息准确地描述了这一点——您试图使用一种不存在的方法来比较两个数字
使用排序描述符时,只有在不希望使用升序值所隐含的公共排序时,才需要构造函数的选择器
和比较器
版本。对于数值,如果您只是希望它们按值排序,则不需要任何一种。你可以简单地使用
NSSortDescriptor(key: #keyPath(Club.leaguePoints),
ascending: false)
你的
leaguePoints
属性是数字还是字符串?看起来您正在尝试在数字字段中使用字符串比较方法。调用save时,它可能在保存验证和/或设置默认值nil之前执行一些操作,因此比较有效。@RogleaguePoints
,leagueGoalDiff
和leagueGoalsFor
是Int32
。名称是一个字符串
。我应该如何编写Int32比较?谢谢Tom,将描述符更改为您的建议解决了这个问题。例如:let pointsortdescriptor:NSSortDescriptor=NSSortDescriptor(key:#keyPath(Club.leaguePoints),升序:false)
NSSortDescriptor(key: #keyPath(Club.leaguePoints),
ascending: false)