Swift 核心数据 NSFetchRequest 谓词获取日期时间
Swift core data NSFetchRequest predicate to fetch date time
我有一个名为 UVHour 的数据库 table,其结构如下
我的应用程序将从如下所示的 JSON 填充 UVHour table
[
{
"ORDER": 1,
"DATE_TIME": "SEP/04/2020 04 AM",
"UV_VALUE": 0
},
{
"ORDER": 2,
"DATE_TIME": "SEP/04/2020 05 AM",
"UV_VALUE": 0
}
]
当前 NSFetchRequest
static var uvIndexNowRequest: NSFetchRequest<UVHour>
{
let dateTimeNow = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMM/d/yyyy hh a"
let dateHourStr = formatter.string(from: dateTimeNow)
let request: NSFetchRequest = UVHour.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true)]
// Below code will throw 'NSInvalidArgumentException', reason: 'Unable to parse the format string "date_time == Sep/4/2020 08 PM"
// request.predicate = NSPredicate(format: "date_time == \(dateHourStr)")
return request
}
问题
我如何编写一个谓词来获取当前日期和时间与当前小时匹配的 UVHour?
例子
假设当前日期是 2020 年 9 月 4 日 04:10 上午。
基于以上JSON我会获取如下的UVHour
[
{
"ORDER": 1,
"DATE_TIME": "SEP/04/2020 04 AM",
"UV_VALUE": 0
}
]
我该怎么做?
我能够更新 uvIndexNowRequest 来找到我的解决方案。见下面代码
static var uvIndexNowRequest: NSFetchRequest<UVHour>
{
let dateTimeNow = Date()
let formatter = DateFormatter()
formatter.timeZone = .current
formatter.dateFormat = "MMM/d/yyyy hh a"
// Find date String from 'formatter.dateFormat'
let dateHourStr = formatter.string(from: dateTimeNow)
// Get date from dateHourStr
let someDateTimeOpt = formatter.date(from: dateHourStr)
let request: NSFetchRequest = UVHour.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true)]
if let someDate = someDateTimeOpt {
request.predicate = NSPredicate(format: "date_time == %@", someDate as CVarArg)
}
return request
}
一个更短的替代方案是 NSDate
(!) 的扩展以获得小时。好处是您可以使用计算的 属性 作为键路径。
extension NSDate {
dynamic var isInCurrentHour : Bool {
return Calendar.current.isDate(self as Date, equalTo: Date(), toGranularity: .hour)
}
}
static var uvIndexNowRequest: NSFetchRequest<UVHour>
{
let request: NSFetchRequest = UVHour.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true)]
request.predicate = NSPredicate(format: "date_time.isInCurrentHour == TRUE")
return request
}
我有一个名为 UVHour 的数据库 table,其结构如下
我的应用程序将从如下所示的 JSON 填充 UVHour table
[
{
"ORDER": 1,
"DATE_TIME": "SEP/04/2020 04 AM",
"UV_VALUE": 0
},
{
"ORDER": 2,
"DATE_TIME": "SEP/04/2020 05 AM",
"UV_VALUE": 0
}
]
当前 NSFetchRequest
static var uvIndexNowRequest: NSFetchRequest<UVHour>
{
let dateTimeNow = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMM/d/yyyy hh a"
let dateHourStr = formatter.string(from: dateTimeNow)
let request: NSFetchRequest = UVHour.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true)]
// Below code will throw 'NSInvalidArgumentException', reason: 'Unable to parse the format string "date_time == Sep/4/2020 08 PM"
// request.predicate = NSPredicate(format: "date_time == \(dateHourStr)")
return request
}
问题
我如何编写一个谓词来获取当前日期和时间与当前小时匹配的 UVHour?
例子
假设当前日期是 2020 年 9 月 4 日 04:10 上午。
基于以上JSON我会获取如下的UVHour
[
{
"ORDER": 1,
"DATE_TIME": "SEP/04/2020 04 AM",
"UV_VALUE": 0
}
]
我该怎么做?
我能够更新 uvIndexNowRequest 来找到我的解决方案。见下面代码
static var uvIndexNowRequest: NSFetchRequest<UVHour>
{
let dateTimeNow = Date()
let formatter = DateFormatter()
formatter.timeZone = .current
formatter.dateFormat = "MMM/d/yyyy hh a"
// Find date String from 'formatter.dateFormat'
let dateHourStr = formatter.string(from: dateTimeNow)
// Get date from dateHourStr
let someDateTimeOpt = formatter.date(from: dateHourStr)
let request: NSFetchRequest = UVHour.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true)]
if let someDate = someDateTimeOpt {
request.predicate = NSPredicate(format: "date_time == %@", someDate as CVarArg)
}
return request
}
一个更短的替代方案是 NSDate
(!) 的扩展以获得小时。好处是您可以使用计算的 属性 作为键路径。
extension NSDate {
dynamic var isInCurrentHour : Bool {
return Calendar.current.isDate(self as Date, equalTo: Date(), toGranularity: .hour)
}
}
static var uvIndexNowRequest: NSFetchRequest<UVHour>
{
let request: NSFetchRequest = UVHour.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true)]
request.predicate = NSPredicate(format: "date_time.isInCurrentHour == TRUE")
return request
}