如何使用 Swift 从 CoreData sqlite table 获取特定行数据
How to get specific row data from the CoreData sqlite table using Swift
我是使用 Swift 语言进行 iOS 开发的新手。
我在我的应用程序中使用 CoreData 作为数据库选项。
我正在使用 NSFetchRequest 从 table 中获取记录。我可以从 table 检索所有记录,但无法从同一个 table 检索特定行。
我已经在网上和其他论坛上查找了解决方案,但我无法找到合适的解决方案。
我只想在 Swift 中实现它。我不想添加 sqlite 库或 Bridging-wrapper (Objective - C),这将是我实现它的最后选择。
任何链接或教程或建议都会有所帮助。
NSFetchRequest 也支持 NSPredicate。使用 NSPredicate,您可以从 Core Data 中选择您需要的确切行。
关于 NSPredicate 的更多信息 - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/
您可以通过执行提取请求来提取所需的行,将其转换为数组,然后简单地使用下标检索索引。或者您可以使用唯一 ID 和谓词并在索引 0 处获取对象来缩小结果范围。
if let results = managedObjectContext?.executeFetchRequest(fetchRequest, error: nil) as? [SomeClass] {
let someObject = results[someIndex]
}
正如 Pavel 所说,使用 NSPredicate。这是我的代码中的一个工作示例,它可能会帮助您入门;)我在相关行下面添加了一些评论
var req = NSFetchRequest(entityName: "YourDBTable")
// your db-table goes here
req.sortDescriptors = [NSSortDescriptor(key: "timeMillis", ascending: true)]
// if you want to sort the results
let start = NSNumber(longLong:int64StartValue)
// you need to convert your numbers to NSNumber
let end = NSNumber(longLong:int64EndValue)
let pred = NSPredicate(format:"timeMillis>=%@ AND timeMillis <=%@ AND foreignTableObject=%@",start,end, foreignTableObject)
// if you have relationships, you can even add another NSManagedObject
// from another table as filter (I am doing this with foreignTableObject here)
req.predicate = pred
var fetchedResults = managedContext?.executeFetchRequest(req,error: &error) as! [YourDBTable]?
// YourDBTable-class was created using the Xcode data model -> Editor -> Create NSManagedObject SubClass... - option
if let results = fetchedResults {
// iterate through your results
}
我是使用 Swift 语言进行 iOS 开发的新手。
我在我的应用程序中使用 CoreData 作为数据库选项。
我正在使用 NSFetchRequest 从 table 中获取记录。我可以从 table 检索所有记录,但无法从同一个 table 检索特定行。 我已经在网上和其他论坛上查找了解决方案,但我无法找到合适的解决方案。
我只想在 Swift 中实现它。我不想添加 sqlite 库或 Bridging-wrapper (Objective - C),这将是我实现它的最后选择。
任何链接或教程或建议都会有所帮助。
NSFetchRequest 也支持 NSPredicate。使用 NSPredicate,您可以从 Core Data 中选择您需要的确切行。
关于 NSPredicate 的更多信息 - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/
您可以通过执行提取请求来提取所需的行,将其转换为数组,然后简单地使用下标检索索引。或者您可以使用唯一 ID 和谓词并在索引 0 处获取对象来缩小结果范围。
if let results = managedObjectContext?.executeFetchRequest(fetchRequest, error: nil) as? [SomeClass] {
let someObject = results[someIndex]
}
正如 Pavel 所说,使用 NSPredicate。这是我的代码中的一个工作示例,它可能会帮助您入门;)我在相关行下面添加了一些评论
var req = NSFetchRequest(entityName: "YourDBTable")
// your db-table goes here
req.sortDescriptors = [NSSortDescriptor(key: "timeMillis", ascending: true)]
// if you want to sort the results
let start = NSNumber(longLong:int64StartValue)
// you need to convert your numbers to NSNumber
let end = NSNumber(longLong:int64EndValue)
let pred = NSPredicate(format:"timeMillis>=%@ AND timeMillis <=%@ AND foreignTableObject=%@",start,end, foreignTableObject)
// if you have relationships, you can even add another NSManagedObject
// from another table as filter (I am doing this with foreignTableObject here)
req.predicate = pred
var fetchedResults = managedContext?.executeFetchRequest(req,error: &error) as! [YourDBTable]?
// YourDBTable-class was created using the Xcode data model -> Editor -> Create NSManagedObject SubClass... - option
if let results = fetchedResults {
// iterate through your results
}