core data - NSBatchDeleteRequest with NSPredicate in swift -
how delete rows coredata using nsbatchdeleterequest nspredicate.
i want delete records older 30 days.
i dont want data in memory , compare , delete, want use nsbatchdeleterequest, in nsbatchupdaterequest.
here code far have written
let date = nsdate(); let yesterday = date.datebyaddingtimeinterval(-24*60*60); let predicate = nspredicate(format: "date > %@", yesterday); let fetchrequest = nsfetchrequest(entityname: "notification"); let batchdelete = nsbatchdeleterequest(fetchrequest: fetchrequest)
please give answers in swift
this simple example. possible rich using of nspredicate
filter request.
let calendar = nscalendar.currentcalendar() let thirtydaysago = calendar.datebyaddingunit(.day, value: -30, todate: nsdate(), options: []) let fetchrequest = nsfetchrequest(entityname: "notification") fetchrequest.predicate = nspredicate(format: "(date >= %@)", thirtydaysago) let deleterequest = nsbatchdeleterequest(fetchrequest: fetchrequest) { try context.executerequest(deleterequest) try context.save() } catch { print (error) }
Comments
Post a Comment