0

I just wanted to do performances test on iOS on my project, and I am a little bit surprised of this behaviour :

In a simple SingleView Application, if I add 1000 UITextField on rect (20, 20, 200, 200) from the viewDidLoad: main controller method, it works great. It's stupid, but it works.

Now i create my class "MyTextField" inherited from UITextField and that implements drawRect: . The drawRect: implementation does nothing and i'm not overriding any other methods of UITextField. I replace my 1000 UITextField with MyTextField class, and surprise : it crashes. Worse, my iPhone reboots !

I don't understand why. According to the Apple documentation, my drawRect does not need to call super. I also try to call super drawRect: but the result is the same. Reboot due to "Receive memory warning".

Is there an explanation to this please ?

EDIT : to be clear :

It crashed (and my iPhone reboots) :

@implementation MyTextField

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}

@end

It crashed too (and my iPhone reboots) :

@implementation MyTextField

-(void)drawRect:(CGRect)rect {
    // or does nothing
}

@end

It works :

@implementation MyTextField

@end

Here is my ViewController :

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"######### START ###########");
    for (int i = 0 ; i < 1000 ; i++) {
        MyTextField *tf = [[MyTextField alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
        [self.view addSubview:tf];
    }
    NSLog(@"######### END ###########");
}

It does nothing else

QLag
  • 823
  • 1
  • 13
  • 33
  • 1
    Take a look at this question for explanation about performance when implementing drawRect : http://stackoverflow.com/questions/18748276/why-an-empty-implementation-of-drawrrect-will-adversely-affects-performance-dur – KIDdAe Mar 18 '15 at 15:30

2 Answers2

1

They are warning you that blank implementation of drawRect affects performance

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
IxPaka
  • 1,990
  • 1
  • 16
  • 18
  • Okay If i do nothing in "drawRect:". But If i call "[super drawRect:rect]" it crashed whereas it should be the same of not implementing "drawRect:" And not implementing" drawRect:" works... – QLag Mar 18 '15 at 10:27
  • @QLag i dont understand you, how implementing drawRect and calling super should be same as not implementing it at all? – IxPaka Mar 18 '15 at 10:36
  • If i dont implement drawRect:, it should automatically call the drawRect of the UITextField, isn't it ? If i implement it to call the super, shouldn't be the same ? – QLag Mar 18 '15 at 10:59
  • Okay so by implementing drawRect, iOs considers it is needed to create a Core Graphics Context that is not created by default by the parents of MyTextfield. By the way, forget my UITextfield and consider adding 1000 UITextfield it works. Now set a text to the 1000 textfields : it crashed too because it need a graphic context. It's surely an UIView optimization ... – QLag Mar 18 '15 at 16:49
0

I think there's no guarantee that super has an implementation of drawRect. So calling [super drawRect:rect] might cause a crash.

The reason why it affects the performance, is because the view normally doesn't use drawRect to draw itself. If you implement drawRect, the GPU needs to offload this part to the CPU (called offscreen rendering).

I guess you think implementing drawRect overwrites the method in a superclass. But you should think more about the superclass checking if drawRect exists (e.g. respondsToSelector) and calling it only in this case.

Thyraz
  • 2,412
  • 2
  • 21
  • 23
  • It's interesting but i think that if drawrect doesn't not exist in any of super classes of MyTextField, i should have a "unrecognized selector sent to instance" instead of Receive Memory Warning. – QLag Mar 18 '15 at 11:00
  • Yes, you're right. Also the docs say "the Default implementation does nothing" and not that it doesn't exist. – Thyraz Mar 18 '15 at 11:08