如何在后台执行计数请求,下面的代码严重阻塞了我的 UI。我试图实现 NSAsynchronousFetchRequest
但我无法将它放在一起。任何帮助将不胜感激!
let NewMessage: NSFetchRequest<Mesages> = Mesages.fetchRequest()
NewMessage.predicate = NSPredicate(format: "timestamp = %@", date)
do {
//context.fetch blocking the main thread
let checkMessages = try context.fetch(NewMessage)
if checkMessages.count == 0 {
print('no duplicates')
}
} catch {
}
使用 PrivateQueueConcurrencyType 到本地托管对象并使用 countForFetchRequest 方法而不是 fetch :
let context: NSManagedObjectContext = NSManagedObjectContext.init(concurrencyType: .PrivateQueueConcurrencyType)
// set context with parent child hierarchy.
let NewMessage: NSFetchRequest = NSFetchRequest.init(entityName: "Message")
var error:NSError? ;
let messagesCount = context.countForFetchRequest(NewMessage, error:&error)
print(messagesCount)
关于ios - 如何在后台执行计数获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40557121/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |