gpt4 book ai didi

swift - 过滤 Realm 时显示 NSProgressIndicator

转载 作者:可可西里 更新时间:2023-11-01 02:01:09 25 4
gpt4 key购买 nike

设置:

一个 Swift 3 应用程序,RealmSwift 2.10.1,带有一个按钮、tableview 和一个不确定的旋转进度指示器(旋转的圆圈)

目标:

当用户点击按钮时,查询大型 Realm 数据库(百万条记录)并在等待结果期间显示进度指示器。

问题:

进度指示器在过滤器完成后才会显示。

代码:

//called from button click
func performFilterAction() {
self.filterProgressIndicator.isHidden = false
self.filterProgressIndicator.startAnimation(nil)
let predicate = NSPredicate(format: "location = %@", "US")
let realm = try! Realm()
self.results = realm.objects(MyClass.self).filter(predicate)
self.tableView.reloadData()
self.filterProgressIndicator.isHidden = true
}

上面的代码为美国的所有位置过滤了 Realm,并填充了一个数组,该数组用作 tableView 的数据源。

其他:

另一个问题是,即使进度指示器隐藏在代码末尾,它也不会隐藏在 UI 中。

我试过在后台线程上实现过滤代码:

func performFilterAction() {
self.filterProgressIndicator.isHidden = false
self.filterProgressIndicator.startAnimation(nil)

DispatchQueue.global(qos: .background).async {
let predicate = NSPredicate(format: "location = %@", "US")
let realm = try! Realm()
self.results = realm.objects(MyClass.self).filter(predicate)
DispatchQueue.main.async {
self.tableView.reloadData()
self.filterProgressIndicator.isHidden = true
}
}
}

它在过滤之前确实显示了 progressIndicator,但在 tableView 重新加载时崩溃并出现以下错误

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

另一个尝试:

根据提供的答案,我实现了这样的收集通知

func performFilterAction1() {
self.filterProgressIndicator.isHidden = false
self.filterProgressIndicator.startAnimation(nil)
let predicate = NSPredicate(format: "location = %@", "US")
let realm = try! Realm()
self.results = realm.objects(MyClass.self).filter(predicate)

self.notificationToken = self.results.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
guard let tableView = self?.filterTableView else { return }
switch changes {
case .initial:
// Results are now populated and can be accessed without blocking the UI
tableView.reloadData()
self?.filterProgressIndicator.isHidden = true
break
case .update(_, let deletions, let insertions, let modifications):
break
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
break
}
}
}

这样做的结果:进度指示器在过滤器完成之前不会显示,它只是暂时“闪烁”。

每个请求:回溯

772.00 ms   58.7%   0 s      Main Thread  0x72995
742.00 ms 56.5% 0 s start
742.00 ms 56.5% 0 s main
742.00 ms 56.5% 0 s NSApplicationMain
625.00 ms 47.6% 1.00 ms -[NSApplication run]
377.00 ms 28.7% 0 s -[NSApplication(NSEvent) sendEvent:]
342.00 ms 26.0% 0 s -[NSWindow(NSEventRouting) sendEvent:]
342.00 ms 26.0% 0 s -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:]
342.00 ms 26.0% 0 s -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:]
331.00 ms 25.2% 0 s -[NSControl mouseDown:]
330.00 ms 25.1% 0 s -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:]
330.00 ms 25.1% 0 s -[NSCell trackMouse:inRect:ofView:untilMouseUp:]
319.00 ms 24.2% 0 s _os_activity_initiate_impl
319.00 ms 24.2% 0 s -[NSButtonCell _sendActionFrom:]
319.00 ms 24.2% 0 s -[NSCell _sendActionFrom:]
319.00 ms 24.2% 0 s _os_activity_initiate_impl
319.00 ms 24.2% 0 s __26-[NSCell _sendActionFrom:]_block_invoke
319.00 ms 24.2% 0 s -[NSControl sendAction:to:]
319.00 ms 24.2% 0 s -[NSApplication(NSResponder) sendAction:to:from:]
318.00 ms 24.2% 0 s _os_activity_initiate_impl
259.00 ms 19.7% 0 s @objc FilterVC.filterAction(Any) -> ()
259.00 ms 19.7% 0 s FilterVC.filterAction(Any) -> ()
257.00 ms 19.5% 0 s FilterVC.test() -> ()
238.00 ms 18.1% 0 s Results.count.getter
238.00 ms 18.1% 0 s -[RLMResults count]
238.00 ms 18.1% 0 s -[RLMResults count]::$_1::operator()() const
238.00 ms 18.1% 0 s realm::Results::size()
238.00 ms 18.1% 0 s realm::Query::count(unsigned long, unsigned long, unsigned long) const
238.00 ms 18.1% 0 s realm::Query::aggregate_internal(realm::Action, realm::DataType, bool, realm::ParentNode*, realm::QueryStateBase*, unsigned long, unsigned long, realm::SequentialGetterBase*) const

最佳答案

这是 Realm's collection notifications 的完美用例.这看起来像:

var notificationToken: NotificationToken? = nil
var results: Results<MyClass>? = nil

func performFilterAction() {
filterProgressIndicator.isHidden = false
filterProgressIndicator.startAnimation(nil)

let realm = try! Realm()
results = realm.objects(MyClass.self).filter("location = %@", "US")
notificationToken = results.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
guard let tableView = self?.tableView else { return }
switch changes {
case .initial:
// Results are now populated and can be accessed without blocking the UI
tableView.reloadData()
self.filterProgressIndicator.isHidden = true
break
case .update(_, let deletions, let insertions, let modifications):
// Query results have changed, so apply them to the UITableView
tableView.beginUpdates()
tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
with: .automatic)
tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.endUpdates()
break
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
break
}
}
}

deinit {
notificationToken?.stop()
}

这使您可以在执行初始过滤时显示进度指示器,还可以在对集合中的数据进行更改时提供动画更新。如果您不关心后者,您也可以在 .update 情况下调用 reloadData()

关于swift - 过滤 Realm 时显示 NSProgressIndicator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46265296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com