Search code examples
iosnsattributedstring

NSAttributed string on main thread


I can use this to get all attributes from nsattributed string.

- (void)enumerateAttribute:(NSString *)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(id value, NSRange range, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

May I know it is possible to get on main thread instead of block as in enumerateAttribute?


Solution

  • This is not an asynchronous operation actually. The usingBlock is simply called synchronously as part of the call to enumerateAttribute: and on the same thread where you made that call. See the usingBlock: as an alternative for a for loop that execute.

    If you are not on the main thread then you can use something like this to ensure that:

    dispatch_async(dispatch_get_main_queue(), ^{
       [string enumerateAttribute: ... usingBlock: ^{
          // Code here
       }];
    });