mirror of
https://github.com/status-im/react-native.git
synced 2025-01-28 02:04:55 +00:00
Remove deprecation warning for RCTExecuteOnMainThread
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 https://github.com/facebook/react-native/issues/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 https://github.com/facebook/react-native/pull/11817 Differential Revision: D4402911 fbshipit-source-id: 9fd8b3f50d34984b765fe22b1f4512e103ba55a9
This commit is contained in:
parent
40f2b1bbb7
commit
00d5674474
@ -29,7 +29,7 @@ RCT_EXTERN id RCTJSONClean(id object);
|
|||||||
// Get MD5 hash of a string
|
// Get MD5 hash of a string
|
||||||
RCT_EXTERN NSString *RCTMD5Hash(NSString *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)
|
// the main thread, which is not neccesarily the same thing)
|
||||||
// https://twitter.com/olebegemann/status/738656134731599872
|
// https://twitter.com/olebegemann/status/738656134731599872
|
||||||
RCT_EXTERN BOOL RCTIsMainQueue(void);
|
RCT_EXTERN BOOL RCTIsMainQueue(void);
|
||||||
@ -38,6 +38,10 @@ RCT_EXTERN BOOL RCTIsMainQueue(void);
|
|||||||
// this will execute immediately if we're already on the main queue.
|
// this will execute immediately if we're already on the main queue.
|
||||||
RCT_EXTERN void RCTExecuteOnMainQueue(dispatch_block_t block);
|
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.
|
// Deprecated - do not use.
|
||||||
RCT_EXTERN void RCTExecuteOnMainThread(dispatch_block_t block, BOOL sync)
|
RCT_EXTERN void RCTExecuteOnMainThread(dispatch_block_t block, BOOL sync)
|
||||||
__deprecated_msg("Use RCTExecuteOnMainQueue instead. RCTExecuteOnMainQueue is "
|
__deprecated_msg("Use RCTExecuteOnMainQueue instead. RCTExecuteOnMainQueue is "
|
||||||
|
@ -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)
|
void RCTExecuteOnMainThread(dispatch_block_t block, BOOL sync)
|
||||||
{
|
{
|
||||||
if (RCTIsMainQueue()) {
|
if (RCTIsMainQueue()) {
|
||||||
@ -272,9 +285,9 @@ CGFloat RCTScreenScale()
|
|||||||
static CGFloat scale;
|
static CGFloat scale;
|
||||||
static dispatch_once_t onceToken;
|
static dispatch_once_t onceToken;
|
||||||
dispatch_once(&onceToken, ^{
|
dispatch_once(&onceToken, ^{
|
||||||
RCTExecuteOnMainThread(^{
|
RCTUnsafeExecuteOnMainQueueSync(^{
|
||||||
scale = [UIScreen mainScreen].scale;
|
scale = [UIScreen mainScreen].scale;
|
||||||
}, YES);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return scale;
|
return scale;
|
||||||
@ -289,9 +302,9 @@ CGSize RCTScreenSize()
|
|||||||
static CGSize size;
|
static CGSize size;
|
||||||
static dispatch_once_t onceToken;
|
static dispatch_once_t onceToken;
|
||||||
dispatch_once(&onceToken, ^{
|
dispatch_once(&onceToken, ^{
|
||||||
RCTExecuteOnMainThread(^{
|
RCTUnsafeExecuteOnMainQueueSync(^{
|
||||||
size = [UIScreen mainScreen].bounds.size;
|
size = [UIScreen mainScreen].bounds.size;
|
||||||
}, YES);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user