我正在开发一个需要在后台线程中构建一些图像的应用程序。在此过程中的某个时刻,我需要从 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
和实施:
#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
我这样称呼它:
NSArray *textViewProperties = [textView getValuesFromMainThreadWithSelectors:@selector(text), @selector(font), nil];
获取字体不会给出与获取文本相同的警告,但我认为最好保持一致。