D. *_*ick 4 multithreading objective-c ios ios6
我正在开发一个应用程序,我需要在后台线程中构建一些图像.在这个过程中的某个时刻,我需要从UITextView获取文本.如果我调用UITextview.text,我会收到警告,我的辅助线程不应该纠缠UIKit
这一切都很好,但我需要文本,我无法找出从主线程获得所述文本的合理方法.
我的问题是:有没有人想出一个很好的方法来从后台线程获取UI元素的属性,或者,首先避免这样做的好方法?
我把这个东西放在一起就可以了,但是感觉不对:
@interface SelectorMap : NSObject
@property (nonatomic, strong) NSArray *selectors;
@property (nonatomic, strong) NSArray *results;
@end
@interface NSObject (Extensions)
- (NSArray *)getValuesFromMainThreadWithSelectors:(SEL)selector, ...;
- (void)performSelectorMap:(SelectorMap *)map;
@end
Run Code Online (Sandbox Code Playgroud)
并实施:
#import "NSObject+Extensions.h"
@implementation SelectorMap
@synthesize selectors;
@synthesize results;
@end
@implementation NSObject (Extensions)
- (void)performSelectorMap:(SelectorMap *)map
{
NSMutableArray *results = [NSMutableArray arrayWithCapacity:map.selectors.count];
for (NSString *selectorName in map.selectors)
{
SEL selector = NSSelectorFromString(selectorName);
id result = [self performSelector:selector withObject:nil];
[results addObject:result];
}
map.results = results.copy;
}
- (NSArray *)getValuesFromMainThreadWithSelectors:(SEL)selector, ...
{
NSMutableArray *selectorParms = [NSMutableArray new];
va_list selectors;
va_start(selectors, selector);
for (SEL selectorName = selector; selectorName; selectorName = va_arg(selectors, SEL))
[selectorParms addObject:NSStringFromSelector(selectorName)];
va_end(selectors);
SelectorMap *map = [SelectorMap new];
map.selectors = selectorParms.copy;
[self performSelectorOnMainThread:@selector(performSelectorMap:) withObject:map waitUntilDone:YES];
return map.results;
}
@end
Run Code Online (Sandbox Code Playgroud)
我称之为:
NSArray *textViewProperties = [textView getValuesFromMainThreadWithSelectors:@selector(text), @selector(font), nil];
Run Code Online (Sandbox Code Playgroud)
获取字体并没有给出获取文本的相同警告,但我认为最好保持一致.
bbu*_*bum 15
我尽可能避免使用任何类型的元编程.它完全破坏了编译器重复检查代码的能力,对于启动,它往往是不可读的.
__block NSString* foo;
dispatch_sync(dispatch_get_main_queue(), ^{
foo = [textField ...];
});
Run Code Online (Sandbox Code Playgroud)
请注意,如果不使用ARC,您可能想要copy或者retain在块中然后release或autorelease在本地线程中使用字符串.
| 归档时间: |
|
| 查看次数: |
2635 次 |
| 最近记录: |