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
[deep link][ios] Update openURL method to reflect the result from framework #52643
base: main
Are you sure you want to change the base?
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
338a4e1
to
c762720
Compare
i realized So i guess the return value of it can only reflect the deep link flag value, not the bool result from framework then. |
- (void)openURL:(NSURL*)url | ||
options:(NSDictionary<UIApplicationOpenExternalURLOptionsKey, id>*)options | ||
completionHandler:(void (^)(BOOL success))completion { | ||
if (![self isFlutterDeepLinkingEnabled]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even this is No, we still want to go through plugins, I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why we want to do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for those who use uni-link or their own plugin to handle deeplink, they would set this to no, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes they would set this to no.
but for those using uni-link and didn't set isFlutterDeepLinkingEnabled
flag, the current behavior is already return NO for these methods and don't invoke the framework method. This PR is not changing their experience.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, I misread the code, looks like the plugin would have been called already before reaching here.
@@ -171,7 +188,14 @@ - (BOOL)application:(UIApplication*)application | |||
if ([_lifeCycleDelegate application:application openURL:url options:options]) { | |||
return YES; | |||
} | |||
return [self openURL:url]; | |||
if (![self isFlutterDeepLinkingEnabled]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we still ned this check if we go delegate to other selector in line 194?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just add this so the return value reflect the value of the flag.
options:options | ||
completionHandler:^(BOOL success){ | ||
}]; | ||
return YES; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this method get the result from completion handler only?
Also is implementing this selector optional if we already implement the one with completionhandler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this method get the result from completion handler only?>>
this application:openURL:options: method is non-async, i don't think its return value can be set from the completion handler so i used the deep link flag value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But should this method return before the completion handler is complete? Before, this method didn't return until pushRouteInformation
was actually sent, whereas now it would return instantly.
It seems like you'd want to spin the runloop until the completion handler was done, and then return the value from the completion handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the best practice to "spin the runloop"?
I updated the code to wait for the completion handler but got failures :
Failures for clang-tidy on /Volumes/Work/s/w/ir/cache/builder/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm:
/Volumes/Work/s/w/ir/cache/builder/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm:178:3: error: Waiting on a callback using a semaphore creates useless threads and is subject to priority inversion; consider using a synchronous API or changing the caller to be asynchronous [clang-analyzer-optin.performance.GCDAntipattern,-warnings-as-errors]
178 | dispatch_semaphore_wait(semaphore, 5 * NSEC_PER_SEC);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
- (void)openURL:(NSURL*)url | ||
options:(NSDictionary<UIApplicationOpenExternalURLOptionsKey, id>*)options | ||
completionHandler:(void (^)(BOOL success))completion { | ||
if (![self isFlutterDeepLinkingEnabled]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, I misread the code, looks like the plugin would have been called already before reaching here.
@@ -149,18 +155,29 @@ - (BOOL)openURL:(NSURL*)url { | |||
if (didTimeout) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that this is async, can we get rid of the waitForFirstFrame
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remembered this was added to workaround the issue that when we receive openurl, we don't know what the current state of flutter is in. If we send if before the first frame is drawn, the message will be dropped because the widgetbindingobserver is not yet registered. It is also too late to set initialroute at this point since it is already consumed by the framework.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but this was a long time ago, I am not sure if this is still the case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like FlutterAppDelegate
needs to know a lot of details about the engine, the navigationChannel, and the available method. What if this changed to:
- (void)openURL:(NSURL*)url
options:(NSDictionary<UIApplicationOpenExternalURLOptionsKey, id>*)options
completionHandler:(void (^)(BOOL success))completion {
if (![self isFlutterDeepLinkingEnabled]) {
completion(NO);
} else {
FlutterViewController* flutterViewController = [self rootFlutterViewController];
if (flutterViewController) {
[flutterViewController openDeepLink:url completionHandler:completion];
} else {
FML_LOG(ERROR) << "Attempting to open an URL without a Flutter RootViewController.";
completion(NO);
}
}
}
FlutterViewController_Internal.h
- (void)openDeepLink:(NSURL*)url completionHandler:(void (^)(BOOL success))completion;
and then in FlutterViewController:
- (void)openDeepLink:(NSURL*)url completionHandler:(void (^)(BOOL success))completion {
[_engine.get()
waitForFirstFrame:3.0
callback:^(BOOL didTimeout) {
if (didTimeout) {
FML_LOG(ERROR) << "Timeout waiting for the first frame when launching an URL.";
completion(NO);
} else {
// invove the method and get the result
[[_engine.get() navigationChannel]
invokeMethod:@"pushRouteInformation"
arguments:@{
@"location" : url.absoluteString ?: [NSNull null],
}
result:^(id _Nullable result) {
BOOL success =
[result isKindOfClass:[NSNumber class]] && [result boolValue];
if (!success) {
// Logging the error if the result is not successful
FML_LOG(ERROR) << "Failed to handle route information in Flutter.";
}
completion(success);
}];
}
}];
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, It does seem like the viewcontroller may be a better place to put these logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, this looks good, updated!
// Use a dispatch semaphore to wait for the completion handler | ||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You won't want a dispatch_semaphore on the main thread, that will cause hangs and potentially deadlocks.
The run loop is a processing loop for event handling, and there's one assigned to the main thread. I believe what you want is to kick off the request, and spin the main run loop until it completes, then return the value from this method.
I didn't run this code so you should test it, but something like:
__block BOOL openURLSuccess = NO;
__block BOOL openURLCompleted = NO;
[self openURL:url
options:options
completionHandler:^(BOOL success) {
openURLSuccess = success;
openURLCompleted = YES;
}];
while (!openURLCompleted) {
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
return openURLSuccess;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for educating me about the The run loop
I have some more questions :
- is a while loop a form of busy waiting and will it cause some performance issues? Should i also add a timeout to prevent an infinite loop?
- Looks like the async api [openURL:options:completionHandler:] is newer than the non-async api [application:openURL:options:] , is it possible the async one is replacing the non-async one? if so, it may be ok the async api returns the value from the framework but the old non-async api remains the old behavior to return the flag value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is a while loop a form of busy waiting and will it cause some performance issues? Should i also add a timeout to prevent an infinite loop?
Are you concerned the code you're calling didTimeout
logic won't work? You could pass in that timeout into the view controller method so it's obvious it handles timing out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also a bit concerned " navigationChannel invokeMethod:arguments:result: " will time out 😅 Is it possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add a timeout instead of the while loop, that would work too. And return NO if it times out.
There are a lot of timeouts going on here, it seems like it would be bad if it times out but the pushRouteInformation
request has gone through, but not yet returned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the code, the delay comes from waitForFirstFrame
(3 seconds) + pushRouteInformation
(unknown delay). I'd run it and see how long it takes pushRouteInformation
to call its result
block (you can do so by printing out the time elapsed), then pick the shortest delay that you feel confident that will cover this + 3 seconds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is a while loop a form of busy waiting and will it cause some performance issues?
From the API doc the UI shouldn't be blocked, but please test it out. It will be easier to test it by adding a dummy delay to your completion block, so you have enough time to scroll the screen after the open URL call. Something like:
[self openURL:url
options:options
completionHandler:^(BOOL success) {
// Note: delay this call for 20 seconds for debugging purpose
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
openURLSuccess = success;
openURLCompleted = YES;
}
}];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, i just got my iOS device registered under Google's Apple developer account so i can test deep link on it.
- i tested pushRouteInformation' ant it's under a second. i think 5 seconds in total can cover it.
- you're correct, adding delay will not cause UI to be blocked.
- but it looks like my run loop will have a deadlock with the dispatch_async here: https://github.com/flutter/engine/blob/main/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm#L1428
dispatch_async(dispatch_get_main_queue(), ^{
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me try it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
basically it's like
- (void)fakedispatch:(void (^)(BOOL success))completion {
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
NSLog(@"fakedispatch:completionHandler: ");
completion(YES);
});
}
- (void)fakeRunloop {
__block BOOL completed = NO;
CFTimeInterval startTime = CACurrentMediaTime();
[self fakedispatch:^(BOOL success) {
completed = YES;
}];
while (!completed) {
NSLog(@"runing loop %f", CACurrentMediaTime() - startTime);
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
}
}
and it will be a deadlock
shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm
Outdated
Show resolved
Hide resolved
@jmagman ping for review |
// Not set or NO. | ||
return NO; | ||
// if not set, return NO | ||
return isDeepLinkingEnabled ? [isDeepLinkingEnabled boolValue] : NO; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this can be just isDeepLinkingEnabled.boolValue
since sending message to nil will return NO
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will keep this and flip NO to YES in #52350
while (!openURLCompleted) { | ||
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; | ||
} | ||
return openURLSuccess; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you write a helper to avoid duplicate code?
|
||
while (!openURLCompleted) { | ||
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: If you are iffy about using BOOL flag for completeness, a semaphore version can be (pseudocode):
__block BOOL openURLSuccess = NO;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self openURL: url ... {
openURLSuccess = success;
dispatch_semaphore_signal(semaphore)
}];
while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) {
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
Though a BOOL flag would be equally as good, since callback happens on main thread. I don't have a preference here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I just saw @jmagman's comment on semaphore.
To use semaphore on the same thread, you can't block it due to deadlock, as jenn previously commented. Instead, you pass in DISPATCH_TIME_NOW
in dispatch_semaphore_wait
, and then kick off the runloop so it can pick up the completion handler.
follow up on comments on #52350
framework pr : flutter/flutter#147901
Pre-launch Checklist
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.