Skip to content

Commit

Permalink
finish run completion handler in main thread (#748)
Browse files Browse the repository at this point in the history
Finishing #692 . As it said there, completion handler should run in main thread.
Should fix #746
  • Loading branch information
DanielEliraz authored Jun 23, 2021
1 parent 28efc32 commit 9a670a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,28 @@ - (void)testSetPersentationCompletionHandler_ShouldStoreBlock {
}

- (void)testCompletePresentation_ShouldInvokeBlockWithParams {
XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
__block UNNotificationPresentationOptions presentationOptions;
void (^testBlock)(UNNotificationPresentationOptions) = ^void(UNNotificationPresentationOptions options) {
presentationOptions = options;
[expectation fulfill];
};
[_store setPresentationCompletionHandler:testBlock withCompletionKey:@"presentationTestBlock"];
[_store completePresentation:@"presentationTestBlock" withPresentationOptions:UNNotificationPresentationOptionAlert];
[self waitForExpectationsWithTimeout:1 handler:nil];
XCTAssertEqual(presentationOptions, UNNotificationPresentationOptionAlert);
}

- (void)testCompletePresentation_ShouldRemoveBlock {
XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
__block UNNotificationPresentationOptions presentationOptions;
void (^testBlock)(UNNotificationPresentationOptions) = ^void(UNNotificationPresentationOptions options) {
presentationOptions = options;
[expectation fulfill];
};
[_store setPresentationCompletionHandler:testBlock withCompletionKey:@"presentationTestBlock"];
[_store completePresentation:@"presentationTestBlock" withPresentationOptions:UNNotificationPresentationOptionAlert];
[self waitForExpectationsWithTimeout:1 handler:nil];
XCTAssertNil([_store getPresentationCompletionHandler:@"presentationTestBlock"]);
}

Expand Down
12 changes: 8 additions & 4 deletions lib/ios/RNNotificationsStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,20 @@ - (void)completeAction:(NSString *)completionKey {
- (void)completePresentation:(NSString *)completionKey withPresentationOptions:(UNNotificationPresentationOptions)presentationOptions {
void (^completionHandler)() = (void (^)(UNNotificationPresentationOptions))[_presentationCompletionHandlers valueForKey:completionKey];
if (completionHandler) {
completionHandler(presentationOptions);
[_presentationCompletionHandlers removeObjectForKey:completionKey];
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(presentationOptions);
[_presentationCompletionHandlers removeObjectForKey:completionKey];
});
}
}

- (void)completeBackgroundAction:(NSString *)completionKey withBackgroundFetchResult:(UIBackgroundFetchResult)backgroundFetchResult {
void (^completionHandler)() = (void (^)(UIBackgroundFetchResult))[_backgroundActionCompletionHandlers valueForKey:completionKey];
if (completionHandler) {
completionHandler(backgroundFetchResult);
[_backgroundActionCompletionHandlers removeObjectForKey:completionKey];
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(backgroundFetchResult);
[_backgroundActionCompletionHandlers removeObjectForKey:completionKey];
});
}
}

Expand Down

0 comments on commit 9a670a6

Please sign in to comment.