如何从swift 3中的核心数据中删除对象 [英] how can I delete object from core data in swift 3
本文介绍了如何从swift 3中的核心数据中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我如何保存数据到核心,现在我想删除整个数据的特定对象。我可以这样做。这是我如何保存数据
func saveProductDetails(product:Product){
pre>
//设置user_id
product.user_id = UserDefaults.sharedInstace.getCustomerId()
//在本地保存数据
let entity = NSEntityDescription.entityForName(Product,inManagedObjectContext:self.writeContext)
let category = NSEntityDescription.entityForName(Category,inManagedObjectContext:self.writeContext)
let brand = NSEntityDescription.entityForName(Brand,inManagedObjectContext:self.writeContext)
var entityProduct = NSManagedObject实体:entity !, insertIntoManagedObjectContext:self.writeContext)
var entityCategory = NSManagedObject(entity:category !, insertIntoManagedObjectContext:self.writeContext)
var entityBrand = NSManagedObject(entity:brand!,insertIntoManagedObjectContext:self.writeContext )
entityProduct = product.settingManagedObject(entityProduct)
entityProduct.setValue(String(Utill.sharedInstace.getTimeStamp()),forKey:time_stamp)
entityProduct.setValue Constant.Status.STATUS_NOT_VERIFIED,forKey:status)
如果product.categoryObject!= nil {
product.categoryObject.user_id = UserDefaults.sharedInstace.getCustomerId();
entityCategory = product.categoryObject.settingManagedObject(entityCategory)
}
如果product.brandObject!= nil {
product.brandObject.user_id = UserDefaults.sharedInstace.getCustomerId ();
entityBrand = product.brandObject.settingManagedObject(entityBrand)
}
entityProduct.setValue(entityCategory,forKey:category)
entityProduct.setValue(entityBrand,forKey :brand)
writeContext.performBlock {
do {
try self.writeContext.save()
self.managedContext .performBlock({
do {
try self.managedContext.save()
} catch let错误为NSError {
print(Could not save \(error) \\(error.userInfo))
}
})
} catch let错误为NSError {
print(Could not save \(error) \(error.userInfo))
return
}
}
}
此Product对象与另外两个对象有关系。而我只想删除特定的对象,而不是全部。比表示删除(其中
product.purchase_id ==selected_purchse_id
)。解决方案检查swift 3核心数据操作的代码
import CoreData
class CoreDataOperations:NSObject {
// MARK:保存数据
func saveData - > Void {
let managedObjectContext = getContext()
let personData = NSEntityDescription.insertNewObject(forEntityName:Person,into:managedObjectContext)as! Person
personData.name =Raj
personData.city =AnyXYZ
do {
try managedObjectContext.save()
print保存!)
} catch让错误为NSError {
print(无法保存\(错误),\(error.userInfo))
} catch {
}
}
// MARK:获取数据
func fetchData() - > Void {
let moc = getContext()
let fetchRequest = NSFetchRequest< NSFetchRequestResult>(entityName:Person)
do {
let fetchedPerson = try moc.fetch(fetchRequest)as! [Person]
print(fetchedPerson.count)
for fetchedPerson {
print(object.name!)
}
} catch {
fatalError(无法获取员工:\(错误))
}
}
// MARK:删除数据记录
func deleteRecords() - > Void {
let moc = getContext()
let fetchRequest = NSFetchRequest< NSFetchRequestResult>(entityName:Person)
let result = try? moc.fetch(fetchRequest)
let resultData = result as! [Person]
for result in resultData {
moc.delete(object)
}
do {
try moc.save )
print(saved!)
} catch let错误为NSError {
print(Could not save \(error),\(error.userInfo))
} catch {
}
}
// MARK :更新数据
func updateRecords() - > Void {
let moc = getContext()
let fetchRequest = NSFetchRequest< NSFetchRequestResult>(entityName:Person)
let result = moc.fetch(fetchRequest)
let resultData = result as! [Person]
对于resultData中的对象{
object.name! =\(object.name!)Joshi
print(object.name!)
}
do {
try moc.save()
print save)
} catch let错误为NSError {
print(无法保存\(错误),\(error.userInfo))
}
b
$ b}
// MARK:获取上下文
func getContext() - > NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
}
您可以从 https://github.com/rajkumar24u/CoreDataOperations 中获取更多信息
this is how I save data to core, now I want to delete specific object from whole data .how can I do that. this is how I save data
func saveProductDetails(product : Product) { // set user_id product.user_id = UserDefaults.sharedInstace.getCustomerId() //save data locally for the time been let entity = NSEntityDescription.entityForName("Product", inManagedObjectContext: self.writeContext) let category = NSEntityDescription.entityForName("Category", inManagedObjectContext: self.writeContext) let brand = NSEntityDescription.entityForName("Brand", inManagedObjectContext: self.writeContext) var entityProduct = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:self.writeContext) var entityCategory = NSManagedObject(entity: category!, insertIntoManagedObjectContext:self.writeContext) var entityBrand = NSManagedObject(entity: brand!,insertIntoManagedObjectContext: self.writeContext) entityProduct = product.settingManagedObject(entityProduct) entityProduct.setValue(String(Utill.sharedInstace.getTimeStamp()), forKey: "time_stamp") entityProduct.setValue(Constant.Status.STATUS_NOT_VERIFIED, forKey: "status") if product.categoryObject != nil{ product.categoryObject.user_id = UserDefaults.sharedInstace.getCustomerId(); entityCategory = product.categoryObject.settingManagedObject(entityCategory) } if product.brandObject != nil{ product.brandObject.user_id = UserDefaults.sharedInstace.getCustomerId(); entityBrand = product.brandObject.settingManagedObject(entityBrand) } entityProduct.setValue(entityCategory, forKey:"category") entityProduct.setValue(entityBrand, forKey: "brand") writeContext.performBlock { do { try self.writeContext.save() self.managedContext.performBlock({ do{ try self.managedContext.save() } catch let error as NSError { print("Could not save \(error), \(error.userInfo)") } }) } catch let error as NSError { print("Could not save \(error), \(error.userInfo)") return } } }
this Product object has relationship to two others. and I want to delete only specific object , not all. than means delete (which the
product.purchase_id == "selected_purchse_id"
), in tableview. how to do that.解决方案Check this code for swift 3 core data operations
import CoreData class CoreDataOperations: NSObject { // MARK: Save data func saveData() -> Void { let managedObjectContext = getContext() let personData = NSEntityDescription.insertNewObject(forEntityName: "Person", into: managedObjectContext) as! Person personData.name = "Raj" personData.city = "AnyXYZ" do { try managedObjectContext.save() print("saved!") } catch let error as NSError { print("Could not save \(error), \(error.userInfo)") } catch { } } // MARK: Fetching Data func fetchData() -> Void { let moc = getContext() let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Person") do { let fetchedPerson = try moc.fetch(fetchRequest) as! [Person] print(fetchedPerson.count) for object in fetchedPerson { print(object.name!) } } catch { fatalError("Failed to fetch employees: \(error)") } } // MARK: Delete Data Records func deleteRecords() -> Void { let moc = getContext() let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Person") let result = try? moc.fetch(fetchRequest) let resultData = result as! [Person] for object in resultData { moc.delete(object) } do { try moc.save() print("saved!") } catch let error as NSError { print("Could not save \(error), \(error.userInfo)") } catch { } } // MARK: Update Data func updateRecords() -> Void { let moc = getContext() let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Person") let result = try? moc.fetch(fetchRequest) let resultData = result as! [Person] for object in resultData { object.name! = "\(object.name!) Joshi" print(object.name!) } do{ try moc.save() print("saved") }catch let error as NSError { print("Could not save \(error), \(error.userInfo)") } } // MARK: Get Context func getContext () -> NSManagedObjectContext { let appDelegate = UIApplication.shared.delegate as! AppDelegate return appDelegate.persistentContainer.viewContext } }
You can get more from https://github.com/rajkumar24u/CoreDataOperations
这篇关于如何从swift 3中的核心数据中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文