NSOperation: addubview mainstream and sluggish

I performed the following NSOperation to draw Ncustom views

- (void)main {

    for (int i=0; i<N; i++) {

       << Alloc and configure customView #i >>
       //(customView is a UIView with some drawing code in drawrect)

       [delegate.view addSubview:customView];

    }

    NSLog(@"Operation completed");
}

in the drawRect method for customView i have

- (void)drawRect {

    <<Drawing code>>

    NSLog(@"Drawed");
    delegate.drawedViews++;

    if (delegate.drawedViews==VIEWS_NUMBER) {
        [delegate allViewsDrawn];
    }
}

In this way, the delegate is notified when all views are displayed.

The problem is that after the "Operation Complete" journal, it takes about 5 seconds before I can see the first "Drawn" journal.

Why is this happening? And anyway, how should I behave to figure out which line of code takes so long?

------ EDIT ------

Sometimes (for example, 1 out of 10 times) I get glitches in this because I should not call addsubviewfrom NSOperation, since it is not thread safe. So I switched to

[delegate.view  performSelectorOnMainThread:@selector(addSubview:) withObject:customView waitUntilDone:NO];

, ! 5 , .

?

+5
1

, NSOperation ""

dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(main_queue, ^{

    [self createCustomViews];

    dispatch_async(main_queue, ^{

        [self addAnotherCustomViewToView];

    });
});
+5

All Articles