Skip to content

Commit

Permalink
Remove deprecation warning for RCTExecuteOnMainThread
Browse files Browse the repository at this point in the history
Summary:
As per janicduplessis recommendation, provide a new synchronous method to replace the necessary synchronous calls and use a warning in the comments (and method name).

**Motivation**

There are currently a number of XCode warnings that show up in a fresh 0.40 install of a react native project. While the project can still be run, this contributes negatively to the development experience -- valid warnings may be ignored and new ones may be added as per https://en.wikipedia.org/wiki/Broken_windows_theory

This addresses one of the warnings, by providing the functionality of a deprecated method in two specific cases where we can't avoid doing synchronous work on the main queue. See #11736 for more context.

**Test plan (required)**

I ran a project that relied on screen size and it didn't crash...happy to do more involved testing if someone can share better methodology.
Closes #11817

Differential Revision: D4402911

fbshipit-source-id: 9fd8b3f50d34984b765fe22b1f4512e103ba55a9
  • Loading branch information
neilsarkar authored and facebook-github-bot committed Jan 11, 2017
1 parent 40f2b1b commit 00d5674
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion React/Base/RCTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ RCT_EXTERN id RCTJSONClean(id object);
// Get MD5 hash of a string
RCT_EXTERN NSString *RCTMD5Hash(NSString *string);

// Check is we are currently on the main queue (not to be confused with
// Check if we are currently on the main queue (not to be confused with
// the main thread, which is not neccesarily the same thing)
// https://twitter.com/olebegemann/status/738656134731599872
RCT_EXTERN BOOL RCTIsMainQueue(void);
Expand All @@ -38,6 +38,10 @@ RCT_EXTERN BOOL RCTIsMainQueue(void);
// this will execute immediately if we're already on the main queue.
RCT_EXTERN void RCTExecuteOnMainQueue(dispatch_block_t block);

// Legacy function to execute the specified block on the main queue synchronously.
// Please do not use this unless you know what you're doing.
RCT_EXTERN void RCTUnsafeExecuteOnMainQueueSync(dispatch_block_t block);

// Deprecated - do not use.
RCT_EXTERN void RCTExecuteOnMainThread(dispatch_block_t block, BOOL sync)
__deprecated_msg("Use RCTExecuteOnMainQueue instead. RCTExecuteOnMainQueue is "
Expand Down
21 changes: 17 additions & 4 deletions React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ void RCTExecuteOnMainQueue(dispatch_block_t block)
}
}

// Please do not use this method
// unless you know what you are doing.
void RCTUnsafeExecuteOnMainQueueSync(dispatch_block_t block)
{
if (RCTIsMainQueue()) {
block();
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
block();
});
}
}

void RCTExecuteOnMainThread(dispatch_block_t block, BOOL sync)
{
if (RCTIsMainQueue()) {
Expand All @@ -272,9 +285,9 @@ CGFloat RCTScreenScale()
static CGFloat scale;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
RCTExecuteOnMainThread(^{
RCTUnsafeExecuteOnMainQueueSync(^{
scale = [UIScreen mainScreen].scale;
}, YES);
});
});

return scale;
Expand All @@ -289,9 +302,9 @@ CGSize RCTScreenSize()
static CGSize size;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
RCTExecuteOnMainThread(^{
RCTUnsafeExecuteOnMainQueueSync(^{
size = [UIScreen mainScreen].bounds.size;
}, YES);
});
});

return size;
Expand Down

0 comments on commit 00d5674

Please sign in to comment.