CoreData里的增删改查

本文详细介绍了在Swift的CoreData框架下如何进行增、删、改、查操作。通过具体的代码示例,展示了如何插入数据、删除单条或多条数据、更新数据以及查询数据的方法。同时,强调了context在CoreData中的重要性,将其比喻为一个大仓库,存储并管理所有的数据。此外,还提及了CoreData内置的fetch、NSPredicate和delete等实用函数。

沿着上篇 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都是很好用的方法

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值