简体   繁体   English

正确地使用onUpgrade(和内容提供者)来处理更新而不阻塞主线程,“ Loader”是否毫无意义?

[英]Correctly using onUpgrade (and content providers) to handle updates without blocking the main thread, are `Loader`s pointless?

This is one of the questions that involves crossing what I call the "Hello World Gulf" I'm on the "Hello world" I can use SQLite and Content Providers (and resolvers) but I now need to cross to the other side, I cannot make the assumption that onUpgrade will be quick. 这是涉及跨越所谓的“ Hello World Gulf”的问题之一,我在“ Hello world”中可以使用SQLite和内容提供程序(和解析程序),但现在我需要越过另一面,我无法假设onUpgrade将很快。

Now my go-to book (Wrox, Professional Android 4 development - I didn't chose it because of professional, I chose it because Wrox are like the O'Reilly of guides - O'Reilly suck at guides, they are reference book) only touches briefly on using Loader s, so I've done some searching, some more reading and so forth. 现在我要读的书(Wrox,专业的Android 4开发-我没有选择它是因为专业,我之所以选择它是因为Wrox就像指南的O'Reilly-O'Reilly吸吮指南,它们都是参考书)仅简要介绍了使用Loader的过程,因此我进行了一些搜索,更多的阅读等等。

I've basically concluded a Loader is little more than a wrapper, it just does things on a different thread, and gives you a callback (on that worker thread) to process things in, it gives you 3 steps, initiating the query, using the results of the query, and resetting the query. 我已经基本得出结论, Loader只不过是一个包装程序,它只是在另一个线程上执行操作,并为您提供了一个回调(在该工作线程上)以处理内容,它为您提供了3个步骤,启动查询,使用查询的结果,并重置查询。

This seems like quite a thin wrapper, so question 1: 这似乎很薄,所以问题1:

Why would I want to use Loader s? 为什么要使用Loader

I sense I may be missing something you see, most "utilities" like this with Android are really useful if you go with the grain so to speak, and as I said Loaders seem like a pretty thin wrapper, and they force me to have callback names which could become tedious of there are multiple queries going on 我觉得我可能会遗漏一些您看到的东西,如果您使用谷歌的话,大多数类似Android的“实用程序”都非常有用,正如我所说的,Loaders看起来很薄,并且它们迫使我进行回调名称可能会变得乏味,因为存在多个查询

http://developer.android.com/reference/android/content/Loader.html http://developer.android.com/reference/android/content/Loader.html

Reading that points out that "they ought to monitor the data and act upon changes" - this sounds great but it isn't obvious how that is actually done (I am thinking about database tables though) 阅读中指出“他们应该监视数据并根据更改采取行动”-听起来不错,但实际上如何实现尚不明确(尽管我正在考虑数据库表)

Presentation 介绍

How should this alter the look of my application? 这应该如何改变我的应用程序的外观? Should I put a loading spinning thing (I'm not sure on the name, never needed them before) after a certain amount of time post activity creation? 在创建活动后一定时间后,是否应该放置一个旋转的东西(我不确定名称,以前从未用过)? So the fragment is blank, but if X time elapses without the loader reporting back, I show a spiny thing? 因此该片段是空白的,但是如果X时间过去了而没有加载程序报告,我会显示出一个棘手的东西吗?

Other operations 其他作业

Loaders are clearly useless for updates and such, their name alone tells one this much, so any nasty updates and such would have to be wrapped by my own system for shunting work to a worker thread. 装载程序显然对更新毫无用处,仅它们的名字就可以说明一个问题,因此任何讨厌的更新等都必须由我自己的系统包装才能将工作分流到工作线程。 This further leads me to wonder why would I want loaders? 这进一步使我想知道为什么还要装载机?

What I think my answer is Some sort of wrapper (at some level, content provider or otherwise) to do stuff on a worker thread will mean that the upgrade takes place on that thread, this solves the problem because ... well that's not on the main thread. 我认为我的回答是 ,在工作线程上执行某种包装(某种程度上是从内容提供程序或其他级别),这意味着升级是在该线程上进行的,这解决了问题,因为...主线程。

If I do write my own I can then (if I want to) ensure queries happen in a certain order, use my own data-structures (rather than Bundle s) it seems that I have better control. 如果我确实编写自己的代码,则可以(如果愿意)确保查询按特定顺序发生,使用我自己的数据结构(而不​​是Bundle ),似乎可以更好地控制。

What I am really looking for 我真正在寻找什么

Discussion, I find when one knows why things are the way they are that one makes less mistakes and just generally has more confidence, I am sure there's a reason Loader s exist, and there will be some pattern that all of Android lends itself towards, I want to know why this is. 讨论中,我发现当人们知道为什么事情是这样的时候,犯错的机会减少了,并且总体上更有信心,我确信Loader的存在是有原因的,并且所有Android都会采用某种模式,我想知道为什么会这样。

Example: 例:

Adapters (for ListViews) it's not immediately obvious how one keeps track of rows (insert) why one must specify a default style (and why ArrayAdapter uses toString) when most of the time (in my experience, dare I say) it is subclasses, reading the source code gives one an understanding of what the Adapter must actually do, then I challenge myself "Can I think of a (better) system that meets these requirements", usually (and hopefully) my answer to that converges on how it's actually done. 适配器(用于ListViews)并不清楚如何跟踪行(插入)为什么在大多数时候(以我的经验,我敢说)它必须是子类,为什么必须指定默认样式(以及为什么ArrayAdapter使用toString),阅读源代码可以使您理解适配器实际必须执行的操作,然后挑战自己“我可以考虑一个满足这些要求的(更好的)系统”,通常(并希望)我对此的回答集中在其实际工作上完成。

Thus the "Hello World Gulf" is crossed. 这样,“ Hello World Gulf”就越过了。

I look forward to reading answers and any linked text-walls on the matter. 我期待阅读有关此问题的答案和任何链接的文本墙。

您不应该直接使用Loader,而应该使用LoaderManager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有线程阻塞的主线程回调(Java) - Callbacks to main thread without thread blocking (Java) ExecutorService的超时而没有阻塞主线程 - Timeout for ExecutorService without blocking the main thread 异步加载世界或不阻塞主线程 - load a world asynchronously or without blocking the main thread 等待回调而不会阻塞主线程 - Wait for callback without blocking main thread 为什么这个 Future 的方法会阻塞主线程? - Why this Future's method is blocking main thread? 从可调用线程发出异常信号,而不会阻塞主线程 - Signaling for exception from Callable thread without blocking in main thread 在不阻塞线程的情况下获取AsyncTask的结果 - Get AsyncTask's result without blocking the thread 如何使用 executor.execute 和 future.get() 结束任务(使线程超时)(通过上升中断)而不阻塞主线程 - How to end a task(timeout a thread) (by rising interrupt) using executor.execute and future.get() without blocking the main thread ProcessBuilder:在不阻塞主线程的情况下转发已启动进程的 stdout 和 stderr - ProcessBuilder: Forwarding stdout and stderr of started processes without blocking the main thread 在不阻塞主线程的情况下异步调用方法调用 - Invoke method call asynchronously without blocking main thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM