是否可以在没有UITableView的情况下从Sqlite(和CoreData)检索数据 - Swift 3

时间:2016-09-30 10:59:54

标签: ios sqlite core-data swift3

我有一个大约有1000条记录的数据库(sqlite)。 我想访问这些记录而不使用UITableView(索引),而是使用属性的键(即名称)。

我遇到了很多问题,因为所有教程都在使用index.row

这个逻辑错了吗?我应该使用其他东西吗?

修改

到目前为止我设法做到了这一点:

  // MARK: - Fetched results controller

var predicateString = "some string"


var fetchedResultsController: NSFetchedResultsController<Anatomy> {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }

    let coreDataStack = CoreDataStack()
managedObjectContext = coreDataStack.persistentContainer.viewContext


let fetchRequest: NSFetchRequest<Anatomy> = Event.fetchRequest()
let entity = NSEntityDescription.entity(forEntityName: "Event", in: self.managedObjectContext!)
fetchRequest.entity = entity

myPredicate = NSPredicate(format: "nameEng contains[c] %@", predicateString)
fetchRequest.predicate = myPredicate

fetchRequest.fetchBatchSize = 20

// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "sortID", ascending: false)

fetchRequest.sortDescriptors = [sortDescriptor]

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController

do {
    try _fetchedResultsController!.performFetch()
} catch {
    // Replace this implementation with code to handle the error appropriately.
    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    let nserror = error as NSError
    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}

return _fetchedResultsController!
}

var _fetchedResultsController: NSFetchedResultsController<Anatomy>? = nil

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
   //do sth
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    //do sth
}

但我现在遇到了麻烦:

   let object = self.fetchedResultsController......  as! Event //cannot cast this to Event and don t know how to get the values now.... 

            print(object.buttonTag)
//this is where indexPath comes and "messes" things around for me....

2 个答案:

答案 0 :(得分:0)

如果您没有使用tableView,我不会打扰FRC - 标准提取应该足够了:

let coreDataStack = CoreDataStack()
managedObjectContext = coreDataStack.persistentContainer.viewContext

let fetchRequest = Event.fetchRequest()

let myPredicate = NSPredicate(format: "nameEng contains[c] %@", predicateString)
fetchRequest.predicate = myPredicate

do {
    let results = try context.fetch(fetchRequest) as! [Event]
    let myEvent = results[0]
    // Do whatever you need with the Event...

} catch {
    // Replace this implementation with code to handle the error appropriately.
    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    let nserror = error as NSError
    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}

答案 1 :(得分:-1)

您根本不必使用UITableView。

以下是我正在使用的一些指南

这是教程如何与Swift一起使用 https://www.raywenderlich.com/123579/sqlite-tutorial-swift

在这里你可以参考详细的SQLite参考: http://www.tutorialspoint.com/sqlite/index.htm

在这里,您可以在使用时参考规范sqlite函数: https://www.sqlite.org/

让我知道您还需要做些什么。 顺便说一下,您需要为数据库中的每个模式定义自己的模型类。 你应该通过sqlite c API获取所有列。

如果您使用的是Objective-C,则更容易使用此API。

我希望这会对你有所帮助。