English 中文(简体)
如何把各种要素(标签数据)装入一种观点?
原标题:How to lazy/async load various elements(data in labels) in a View?
  • 时间:2012-05-04 19:08:40
  •  标签:
  • ios

I have a ViewController with various labels. Each of these labels get dynamically populated at the run time based on various regex parsing logic running on an html page. The problem is, each regex match is taking 2-3 seconds and I have 8 such labels so that means i have to wait somewhere around 20-25 seconds before the view shows up!

这是一个非常坏的用户经验。 我希望这不会给用户造成痛苦,因此,希望每张标签在经过处理后和当其收到数据时独立装上,而不要等到所有8个标签上填写。

第5章能够做到这一点的任何方式?

最佳回答
  1. Create a separate function which calculates the values that you need.
    (You probably already have this anyway for code readability/maintainability.)
  2. Run this thread in a background thread.
  3. When you are ready to actually set the text, make sure that you do it on the main thread:

例如:

- (void)calculateLabelText {
    NSString *label1Text = // However you calculate this...
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        self.label1.text = label1Text;
    });

    NSString *label2Text = // However you calculate this...
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        self.label2.text = label2Text;
    });
}

鉴于DidLoad,增加:

[self performSelectorInBackground:@selector(calculateLabelText) withObject:nil];
问题回答

大型中央分遣队。 它为你处理点和校对等。 没有必要仅仅为一次发生的一套行动制定一种方法,而不管怎么说,<编码>发送_async()的速度要快于在背景线上进行挑选,而且你必须保持现有的代码结构——你只是把它总结在一个赢得主线的冰块上:

/ 参考全球并行问题(无需建立我们自己)。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

每一标签的操作(2008年)

dispatch_async(queue, ^{
     // your regex here for one 

     // execute back in the main thread since UIKit only operates in the main thread.

    dispatch_async(dispatch_get_main_queue(), ^{
        [myLabel setText:<result of operations>];
    iii);
iii); 

iii

如果你想这样做,你必须特别注意使你的法典分离。 一部分是数据负荷工作,另一部分是控制数据。 并且,你们必须保证,你能够用多管线冷却。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签