sizeWithFont in MultiThread Crash!

梦想与她 提交于 2019-12-06 03:42:25

As a rule, you should not invoke UIKit methods [1] from a separate thread. It does not matter if you are taking locks, this is a non-starter.

When you are using multi-threaded applications, you need to make sure that any code that touches any UIKit objects executes on the main thread. This is achieved by using the performSelectorOnMainThread:withObject:waitUntilDone: method which invokes the given selector on the main thread:

http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone:

Or in MonoTouch: foo.InvokeOnMainThread (delegate { your_code_here });

[1] With iOS 4.0 the rule is relaxed for a handful of APIs.

  • Formatting, please.

  • "Solving" a multi-threaded problem by placing random locks around objects is never the right answer. Not ever. Multi-threading requires systemic design of your application.

  • If a lock does "fix" the problem, showing what you locked and how is critical to assessing the situation.

  • Some more symptoms would be helpful. Code, in particular. Code in your questions is very useful.

Given the lack of evidence, I'd wager that your are mutating a string on one thread while trying to grab the size on another. Or the object is being released on one thread while still using it another. Or you are manipulating an object from a secondary thread that isn't thread safe.

I think performSelectorOnMainThread:withObject:waitUntilDone: is correct,

Before, I use a operation to calculate text size, And use waitUntilAllOperationsAreFinished in the main thread to wait for the operation's return,
But if I also use performSelectorOnMainThread:withObject:waitUntilDone in the operation, and set the waitUntilDone parameter to Yes(Because I need the result)
The main thread will be stucked

So now I remove waitUntilAllOperationsAreFinished ,and use a asynchronous object to ensure the operation won't start until the previous one stopped

                    [md removeAllObjects];
                    [md setObject:subString forKey:@"text"];
                    [md setObject:[NSNumber numberWithInt:view_w ] forKey:@"width"];
                    [md setObject:[NSNumber numberWithInt:height_left + font_h ] forKey:@"height"];
                    [self  performSelectorOnMainThread:
                     @selector(calculateTextRegion:)
                                            withObject:md
                                         waitUntilDone:YES];
                    CGSize stringSize = textRegion;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!