coredata 删除
删除
1.把要删除的实体对象传入
class func DBGoodsInfoDeleteObject(obj:GoodsInfo) -> Bool{
let context:NSManagedObjectContext = obj.managedObjectContext!
context.deleteObject(obj)
var error: NSError? = nil
return context.save(&error)
}
2.传入所有数据的数组,和要删除的实体对象的索引. 通过索引找到实体对象并
let object = listArray[deleteIndex] as GoodsInfo
app.managedObjectContext?.deleteObject(object)
app.managedObjectContext!.save(&error)
3. 删除所有数据
class func DBGoodsInfoDeleteAll() -> Bool{
let app = DBAppdalegateObject()
let listArray = DBGoodsInfoReadAllData() as NSArray
var context:GoodsInfo
var successed:Bool = true
var error: NSError? = nil
for context in listArray{
app.managedObjectContext?.deleteObject(context as GoodsInfo)
if app.managedObjectContext!.save(&error){
}else{
successed = false
}
}
return successed
}
************************************************************************************
更新
1. 传入更新的数据 及 需要更新的实体对象 (赋值后保存)
class func DBGoodsInfoUpdate(dataDic: Dictionary<String,AnyObject>,obj: GoodsInfo) -> Bool{
let app = DBAppdalegateObject()
obj.title = dataDic["title"] as String
obj.price = dataDic["price"] as String
let context:NSManagedObjectContext = obj.managedObjectContext!
context.refreshObject(obj, mergeChanges: true)
var error: NSError? = nil
return context.save(&error)
}