简体   繁体   English

如何使用Swift从CoreData sqlite表中获取特定的行数据

[英]How to get specific row data from the CoreData sqlite table using Swift

I am new to iOS development using Swift language. 我是使用Swift语言进行iOS开发的新手。

I am using CoreData in my app as a database option. 我在我的应用程序中使用CoreData作为数据库选项。

I am using NSFetchRequest for fetching records from the table. 我正在使用NSFetchRequest从表中获取记录。 I can retrieve all records from the table, but I can't retrieve a specific row from the same table. 我可以从表中检索所有记录,但不能从同一表中检索特定行。 I have checked for the solutions online and other forums, but I am unable to get a proper solution for this. 我已经在网上和其他论坛上找到了解决方案,但是无法为此找到合适的解决方案。

I want to implement this in Swift only. 我只想在Swift中实现这一点。 I don't want to add the sqlite library or Bridging-wrapper (Objective - C) which will be my last option to implement this. 我不想添加sqlite库或Bridging-wrapper(目标-C),这将是我实现此目标的最后选择。

Any links or tutorials or suggestions will be helpful. 任何链接或教程或建议都会有所帮助。

NSFetchRequest also support NSPredicate. NSFetchRequest还支持NSPredicate。 With NSPredicate you can choose which exactly rows or row you need from Core Data. 使用NSPredicate,您可以从Core Data中选择所需的确切行。

More about NSPredicate - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/ 有关NSPredicate的更多信息-https: //developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/

You can fetch a desired row by executing a fetch request, casting it as an array, and simply using subscript to retrieve an index. 您可以通过执行获取请求,将其转换为数组并仅使用下标来检索索引来获取所需的行。 Or you can narrow down your results using a unique id and a predicate and getting the object at index 0. 或者,您可以使用唯一的ID和谓词来缩小结果范围,并使对象的索引为0。

if let results = managedObjectContext?.executeFetchRequest(fetchRequest, error: nil) as? [SomeClass] {
    let someObject = results[someIndex]
}

As Pavel said, use NSPredicate. 正如Pavel所说,使用NSPredicate。 Here is a working example from my code, it might help you to get started ;) I added some comments below relevant lines 这是我的代码中的一个有效示例,它可能会帮助您入门;)我在相关行下添加了一些注释

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM