-
删除关联 Delete Rule
-
Cascade
BE2DD438-C4D9-4661-A406-68C9B7D5BBBB.png
-
No Action
3C4C2D1B-1F7F-4707-B1CF-22CB60114EB1.png
-
Deny
4B7AB82F-1289-43C3-A0E2-349DC05001CA.png
-
Nullify
41572FBB-7223-4DFD-88E5-F09BAE35F396.png
-
Cascade
-
多表关联
267F7E33-A8CA-4E35-BB92-5BBBBC970541.png
21D51C83-6626-4F21-B77F-F70DB89FBDB3.png
-
获取上下文
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
- 存
let entityDes = NSEntityDescription.entity(forEntityName: "Clothes", in: context)
let model = Clothes.init(entity: entityDes!, insertInto: context)
model.name = " aaa"
model.price = Int64(arc4random() % 1000 + 1)
model.ccc = "NN"
dataSrouce.append(model)
try? context.save()
- 删除
context.delete(model)
try? context.save()
- 条件删除
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Clothes")
let pre = NSPredicate(format: "price < %d", 300)
fetch.predicate = pre
let deleArray = try? context.fetch(fetch)
for model in deleArray! {
context.delete(model as! Clothes)
dataSrouce.remove(at: dataSrouce.index(of: model as! Clothes)!)
}
try? context.save()
tableView.reloadData()
- 改
let model = dataSrouce[indexPath.row]
model.name = "CC"
model.price = Int64(arc4random() % 10 + 1)
tableView.reloadRows(at: [indexPath], with: .top)
try? context.save()
- 查
// 查询
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Clothes")
// 排序描述对象
let sortDes = NSSortDescriptor(key: "price", ascending: true)
fetch.sortDescriptors = [sortDes]
// 执行查询
// let result : NSPersistentStoreRequest
do {
let result = try context.fetch(fetch)
for model in result {
dataSrouce.append((model as! Clothes))
}
}catch{
print(error)
}
- 谓词过滤,排序操作
- ( >) 、< 、== 、>= 、<= 、!=
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Clothes")
let pre = NSPredicate(format: "price > %d", 4)
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
- Like
let pre = NSPredicate(format: "name like %@","*aaa*")
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
- And 也可以写 &&
let pre = NSPredicate(format: "name like %@ and price = 510","*aaa*")
或者let pre = NSPredicate(format: "name like %@ and price = %i","*aaa*",510)
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
- Or
let pre = NSPredicate(format: "name like %@ or price = %i","*CC*",554)
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
- In
let pre = NSPredicate(format: "name in %@",[" aaa","CC"])
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
- Between
let pre = NSPredicate(format: "price between {300,700}")
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
- endswith
q 结尾的
let pre = NSPredicate(format: "name endswith[d] 'q'")
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
- beginswith
w 开头的
let pre = NSPredicate(format: "name beginswith[d] 'w'")
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
- contains
let pre = NSPredicate(format: "name contains[d] 'A'")
fetch.predicate = pre
let array = try? context.fetch(fetch)
for model in array! {
print((model as! Clothes).price)
}
网友评论