Iphone Dev: Invalid Context CGContextDrawLayerInRect

iphone

I have a uiscrollview within three subUIViews 1, 2, 3. I load content for subUIView1, 2,3 dynamically. Here are steps

  1. I create scrollview with these 3
    subUIview and with a default size
    for each subUIView
  2. I create an operation for each subUIView, let us say Operation 1,2,3. add to operation queue

  3. every Operation (operation 1) calculate the content that will be later draw in related subUIView (subview 1). after calculate completed, create CGLayer and attach it with UISubview1. reset all UIsubviews frame and bound and layout scrollview subviews and setNeedDisplay for related subUIview1. then move to next operation

  4. in SubUIView drawRect method, i call CGLayerDrawInRect.

then occasionly i
get this error Invalid Context.
sometimes, it work fine, no error sometime it happened

Any idea about this error will be welcome.

.

Best Answer

Take a very long time to debug and finally find what is the problem and resolved. Here is the thing

  1. When operation 1 setNeedDisplay sent out, drawRect have not called yet

  2. Once drawRect is called, operation 2 is already running, during drawRect running,operation 2 somehow change subview1,2,3, layout and frame at the same time.

  3. So, the bad thing happened, context will become invalid that time because frame are changing, and also worst, you may receive exe_bad_access false message

  4. Resolve solution, i set a singal to make sure operation 1 drawRect is done then let operation 2 start run.

    I have to say XCode is terrible development tools compare what MS studio tools. It gives message like invalid Context or exe_bad_access randomly. where is the productivity for our poor developer. However, Iphone OSX is much better than MS windows mobile. that is why i am working hard to use XCode creatively.

Related Topic