我正在尝试从当前日期开始14天之前删除数据。对于前。如果今天是6月20日,我想在6月6日之前删除记录。 (可能是6月6日至6月1日)
当前我正在执行此操作,但是它删除了今天从记录中删除的14天
func removeOldNotifications(_ days: Int) {
guard let fromSearchDate = Calendar.current.date(byAdding: .day, value: -14, to: Date()) else {
return
}
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MMM-dd HH:mm:ss" // Only Date Fetch No need to check Time
let fromSearchDateString = dateFormatterGet.string(from: fromSearchDate)
print("*** Delete From Date String: \(fromSearchDateString)")
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "NotificationTrack")
fetchRequest.returnsObjectsAsFaults = false
let predicaate = NSPredicate(format: "notificationSendTimeStamp < %@", fromSearchDateString)
fetchRequest.predicate = predicaate
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try context.execute(deleteRequest)
try context.save()
print("Deleted Old Core data objects from Entity ")
} catch let error {
print("Detele Old data in error :", error)
}
}
如何在14天之前删除数据? 仅当我对设置日期感到困惑时,删除逻辑才完成。
答案 0 :(得分:1)
假设您有一个如下所示的Record对象:
@IBDesignable
class CustomBg:UIView {
private lazy var curvedLayer: CAShapeLayer = {
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.yellow.cgColor
shapeLayer.lineWidth = 4
shapeLayer.strokeColor = UIColor.green.cgColor
return shapeLayer
}()
//MARK:- inializer
override init(frame: CGRect) {
super.init(frame: frame)
layer.addSublayer(curvedLayer)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
layer.addSublayer(curvedLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
updatePath()
}
func updatePath() {
let centerX = 100
let centerY = 100
let radius1 = CGFloat( 50.0)
let path = UIBezierPath()
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle:0, endAngle: (2 * .pi), clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 150, y: currentPoint.y))
path.addArc(withCenter: CGPoint(x: Int(path.currentPoint.x + radius1) , y: centerY), radius: radius1 , startAngle: .pi, endAngle: (3 * .pi ), clockwise: true)
curvedLayer.path = path.cgPath
}
}
您的代码是正确的,如果您想从现在获取两周前的日期,则可以执行以下操作:
struct Record {
let id: String
let date: Date
}
然后,如果您有一个let nowDate = Date()
let twoWeeksAgoDate = Calendar.current.date(byAdding: .day, value: -14, to: nowDate)!
填充的数组,则只需过滤旧记录,并返回其ID:
Records
这将为您提供早于指定间隔的记录的所有ID (在您的情况下为两周),现在您只需在数据库中或任何位置删除它们已存储。
现在,这一切都取决于您的用例。既然您已经说过您已经知道如何处理删除过程,则不知道您的记录是CoreData记录还是其他内容,但是例如,如果它们确实是CoreData记录,则只需在您的请求中添加谓词即可以便过滤出与您的日期范围匹配的所有记录,例如:
let toBeRemovedIds = records.filter { $0.date < twoWeeksAgoDate }.map { $0.id }
然后再次,只需删除获取的记录。
编辑:
由于您在请求中使用谓词,您的问题正在发生。我建议您在您的 CoreData模型中将let predicate = NSPredicate(format: "date < %@", twoWeeksAgoDate as NSDate)
属性设为notificationSentDate
,而不是Date
。
这将更有意义,并且更不会出错。只需记住在谓词中将日期对象转换为NSDate。无论如何,最终的工作方法将如下所示:
String
答案 1 :(得分:1)
您可以像这样删除startDate
和endDate
之间的任何记录:
let startDate = Calendar.current.date(byAdding: .day, value: -44, to: Date())
let endDate = Calendar.current.date(byAdding: .day, value: -14, to: Date())
或者只需反转删除登录名并删除endDate
之前的所有记录,因为Date
是可比较的,这不会花费很多精力。