Skip to content

[Functions] 'call' API's completion handler should be called on the main thread #14667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions FirebaseFunctions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased
- [fixed] Fix regression from 11.6.0 where `HTTPSCallable` did not invoke
completion block on main thread (#14653).

# 11.10.0
- [added] Streaming callable functions are now supported.

Expand Down
2 changes: 1 addition & 1 deletion FirebaseFunctions/Sources/HTTPSCallable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ open class HTTPSCallable: NSObject {
completion: @escaping (HTTPSCallableResult?,
Error?) -> Void) {
if #available(iOS 13, macCatalyst 13, macOS 10.15, tvOS 13, watchOS 7, *) {
Task {
Task { @MainActor in
do {
let result = try await call(data)
completion(result, nil)
Expand Down
32 changes: 32 additions & 0 deletions FirebaseFunctions/Tests/Integration/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,38 @@ class IntegrationTests: XCTestCase {
XCTAssertEqual(response, expected)
}
}

func testFunctionsReturnsOnMainThread() {
let expectation = expectation(description: #function)
functions.httpsCallable(
"scalarTest",
requestAs: Int16.self,
responseAs: Int.self
).call(17) { result in
guard case .success = result else {
return XCTFail("Unexpected failure.")
}
XCTAssert(Thread.isMainThread)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}

func testFunctionsThrowsOnMainThread() {
let expectation = expectation(description: #function)
functions.httpsCallable(
"httpErrorTest",
requestAs: [Int].self,
responseAs: Int.self
).call([]) { result in
guard case .failure = result else {
return XCTFail("Unexpected failure.")
}
XCTAssert(Thread.isMainThread)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}
}

// MARK: - Streaming
Expand Down
24 changes: 24 additions & 0 deletions FirebaseFunctions/Tests/ObjCIntegration/FIRIntegrationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,28 @@ - (void)testTimeout {
[self waitForExpectations:@[ expectation ] timeout:10];
}

- (void)testFunctionsReturnsOnMainThread {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"scalarTest"];
[function callWithObject:@17
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssert(NSThread.isMainThread);
XCTAssertNotNil(result);
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:10];
}

- (void)testFunctionErrorsOnMainThread {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"httpErrorTest"];
[function callWithObject:@{}
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssert(NSThread.isMainThread);
XCTAssertNotNil(error);
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:10];
}

@end
2 changes: 1 addition & 1 deletion FirebaseFunctions/Tests/Unit/FunctionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class FunctionsTests: XCTestCase {
}

XCTAssertEqual(error as NSError, networkError)

XCTAssert(Thread.isMainThread)
completionExpectation.fulfill()
}

Expand Down
Loading