沿着上篇 https://blog.youkuaiyun.com/SwiftIOS_watsUp/article/details/100006664
本篇继续讲CoreData,主要是如何在CoreData里增删改查(CRUB)
首先是增(insert)
func createObject<T: NSManagedObject>(entity: T.Type, storeType: String = NSSQLiteStoreType) throws -> T {
let context = getContext()
let entityName = String(describing: entity)
guard let description = NSEntityDescription.entity(forEntityName: entityName, in: context) else { throw error.invalidEntityType }
let object = T(entity: description, insertInto: context)
return object
}
这里值得注意的是我是去曾加这一条数据,把这一条数据当成一个object,然后根据这个object的attribute增添数据
删(delete)
func delete<T: NSManagedObject>(entity: T, storeType: String = NSSQLiteStoreType) {
let context = getContext()
context.delete(entity)
}
删比较简单,就只用 .delete就可以了,需要注意这只是删除单一的一条数据,如果想清洗某一个entity的所有数据,可以用for loop
改(update)
查(select)
这里改和查可以一起来讲,首先是查
func fetchAll<T: NSManagedObject>(entity: T.Type, storeType: String = NSSQLiteStoreType) throws -> [T] {
let context = getContext()
let entityName = String(describing: entity)
let entity = NSFetchRequest<T>(entityName: entityName)
let fetchObject = try context.fetch(entity)
return fetchObject
}
这个function主要是用于查询某一个entity里的所有数据
func filter<T: NSManagedObject>(entitiy: T.Type, fieldName: String, valueToFilt: String, storeType: String = NSSQLiteStoreType) throws -> [T] {
let context = getContext()
let entityName = String(describing: entitiy)
let fetchRequest = NSFetchRequest<T>(entityName: entityName)
fetchRequest.predicate = NSPredicate(format: "\(fieldName) == %@", valueToFilt)
let object = try context.fetch(fetchRequest)
return object
}
这个function主要用于去筛选某一个entity里,某一个atrribute,在从中筛选出想要的数据。
以下是完整的code
import Foundation
import CoreData
class CoreDataController {
var storeType: String!
static let coreDataManager = CoreDataController()
private init(){}
lazy var persistentContainer: NSPersistentContainer = {
let persistentContainer = NSPersistentContainer(name: "Model")
let description = persistentContainer.persistentStoreDescriptions.first
description?.type = storeType
persistentContainer.loadPersistentStores(completionHandler: { (_, _) in
})
return persistentContainer
}()
func getPersistentContainer(storyType: String)-> NSPersistentContainer {
self.storeType = storyType
return persistentContainer
}
func getContext(storeType: String = NSSQLiteStoreType) -> NSManagedObjectContext {
let context = getPersistentContainer(storyType: storeType).viewContext
return context
}
func createObject<T: NSManagedObject>(entity: T.Type, storeType: String = NSSQLiteStoreType) throws -> T {
let context = getContext()
let entityName = String(describing: entity)
guard let description = NSEntityDescription.entity(forEntityName: entityName, in: context) else { throw error.invalidEntityType }
let object = T(entity: description, insertInto: context)
return object
}
func delete<T: NSManagedObject>(entity: T, storeType: String = NSSQLiteStoreType) {
let context = getContext()
context.delete(entity)
}
func fetchAll<T: NSManagedObject>(entity: T.Type, storeType: String = NSSQLiteStoreType) throws -> [T] {
let context = getContext()
let entityName = String(describing: entity)
let entity = NSFetchRequest<T>(entityName: entityName)
let fetchObject = try context.fetch(entity)
return fetchObject
}
func filter<T: NSManagedObject>(entitiy: T.Type, fieldName: String, valueToFilt: String, storeType: String = NSSQLiteStoreType) throws -> [T] {
let context = getContext()
let entityName = String(describing: entitiy)
let fetchRequest = NSFetchRequest<T>(entityName: entityName)
fetchRequest.predicate = NSPredicate(format: "\(fieldName) == %@", valueToFilt)
let object = try context.fetch(fetchRequest)
return object
}
enum error: Error {
case invalidEntityType
}
}
下面是测试的结果

总结CoreData有它自己的syntax,需要注意的是我每个function都有用到context,context是从NSpresistentContainer里的viewContext,便于我们方便查看,可以把context想象成一个大仓库,我们可以在这个仓库里查到我们想要的数据。这其中CoreData也有已经build in的function,比如fetch,NSpredict,delete都是很好用的方法
本文详细介绍了在Swift的CoreData框架下如何进行增、删、改、查操作。通过具体的代码示例,展示了如何插入数据、删除单条或多条数据、更新数据以及查询数据的方法。同时,强调了context在CoreData中的重要性,将其比喻为一个大仓库,存储并管理所有的数据。此外,还提及了CoreData内置的fetch、NSPredicate和delete等实用函数。
711

被折叠的 条评论
为什么被折叠?



