我需要完成以下任务:
1) 从sqlite数据库中读取一些数据
2)处理数据
3) 处理后的数据生成一些图表
如果我有一个用户在应用程序中输入很多数据,有一天这个分析会变慢并卡住 UI。
那么,允许用户与 UI 交互的正确处理方式是什么,可以选择取消操作或退出屏幕?
我需要为我的所有任务制作简单的线程,并使用取消事件或标志来停止每个任务?或者还有别的办法吗?
例如:
任务 1:在一个线程中从 sqlite 中读取数据,并在需要时使用标志停止进程。
任务 2:在线程中处理数据,并在需要时使用标志停止进程。
任务 3:将数据传送到第 3 方组件。此时是否可以取消正在其他组件上运行的操作?
我的想法是正确的还是我可以改进某些东西?
最佳答案
这是 Apple 使用 GCD (Grand Central Dispatch) 推荐的最快的方法。它也更容易阅读和理解,因为逻辑是线性的但不会在方法之间拆分。
请注意,它显示了 weakSelf “舞蹈”,如果异步可能比它所调用的 Controller 生命周期更长,那么它是必需的,因此它对其进行弱引用,然后检查它是否存在并保留它以进行更新:
swift 4 和 3
DispatchQueue.global().async() {
print("Work Dispatched")
// Do heavy or time consuming work
// Then return the work on the main thread and update the UI
// Create weak reference to self so that the block will not prevent it to be deallocated before the block is called.
DispatchQueue.main.async() {
[weak self] in
// Return data and update on the main thread, all UI calls should be on the main thread
// Create strong reference to the weakSelf inside the block so that it´s not released while the block is running
guard let strongSelf = self else {return}
strongSelf.method()
}
}
Objective-C
// To prevent retain cycles call back by weak reference
__weak __typeof(self) weakSelf = self; // New C99 uses __typeof(..)
// Heavy work dispatched to a separate thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Work Dispatched");
// Do heavy or time consuming work
// Task 1: Read the data from sqlite
// Task 2: Process the data with a flag to stop the process if needed (only if this takes very long and may be cancelled often).
// Create strong reference to the weakSelf inside the block so that it´s not released while the block is running
__typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf method];
// When finished call back on the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
// Return data and update on the main thread
// Task 3: Deliver the data to a 3rd party component (always do this on the main thread, especially UI).
});
}
});
取消进程的方法是包含一个 BOOL 值并将其设置为如果不再需要正在完成的工作则从主线程停止。但是,这可能不值得,因为除非进行繁重的计算,否则用户不会注意到如此多的后台工作。 为防止保留循环,请使用弱变量,例如:
__weak __typeof(self) weakSelf = self; // Obj-C
[weak self] in
并在 block 内使用强引用调用 weakSelf(以防止调用 VC 已被释放时发生崩溃)。您可以使用确切的类型,如 UIViewController 或 __typeof() 函数(在 C99 中您需要使用 __typeof(..) 但以前您可以直接使用 __typeof(..) )来引用实际类型:
Objective-C
__typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf method];
}
swift 4 和 3
if let weakSelf = self {
weakSelf.method()
}
或
// If not referring to self in method calls.
self?.method()
注意:使用 GCD 或 Grand Central Dispatch,这是最简单的,Apple 推荐的方式,代码流按逻辑顺序排列。
关于objective-c - iOS - 在不卡住 UI 的情况下处理数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903247/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在使用i18n从头开始构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在rubyonrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我最近决定从我的系统中卸载RVM。在thispage提出的一些论点说服我:实际上,我的决定是,我根本不想担心Ruby的多个版本。我只想使用1.9.2-p290版本而不用担心其他任何事情。但是,当我在我的Mac上运行ruby--version时,它告诉我我的版本是1.8.7。我四处寻找如何简单地从我的Mac上卸载这个Ruby,但奇怪的是我没有找到任何东西。似乎唯一想卸载Ruby的人运行linux,而使用Mac的每个人都推荐RVM。如何从我的Mac上卸载Ruby1.8.7?我想升级到1.9.2-p290版本,并且我希望我的系统上只有一个版本。 最佳答案