Bug 245732 - WKDownloadDelegate didFinish is called before download is written to file.
Summary: WKDownloadDelegate didFinish is called before download is written to file.
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit API (show other bugs)
Version: Other
Hardware: iPhone / iPad All
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-09-27 08:02 PDT by Garrigan Stafford
Modified: 2023-10-17 10:16 PDT (History)
4 users (show)

See Also:


Attachments
Sample Project (70.74 KB, application/zip)
2022-09-29 13:46 PDT, Garrigan Stafford
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Garrigan Stafford 2022-09-27 08:02:18 PDT
When using a WKDownload to download a file, the download will succeed and calls -[WKDownloadDelegate downloadDidFinish:] before the download is actually written to file. Consider the following delegate method

- (void)downloadDidFinish:(WKDownload *)download API_AVAILABLE(ios(15.0))
{
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:self.downloadPath.absoluteString];
    UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"Download Succeeded"
                                                                             message:[NSString stringWithFormat:@Exists = %u", fileExists]
                                                                     preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* dismiss = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:self.downloadPath.path];
        NSLog(@"%u",fileExists);
    }];
    [alertController addAction:dismiss];
    if(NSThread.isMainThread)
    {
        [self.webViewController presentViewController:alertController animated:NO completion:nil];
    }
    else
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.webViewController presentViewController:alertController animated:NO completion:nil];
        });
    }
}

Upon a successful download the alert will be presented with "Exists = false" however once the dismiss is pressed we have logged "true". The file will exist after the call but not before.

The delegate should be called AFTER the file has been created so that the delegate can respond to the file be downloaded.
Comment 1 Radar WebKit Bug Importer 2022-09-29 09:26:24 PDT
<rdar://problem/100566434>
Comment 2 Alex Christensen 2022-09-29 12:52:34 PDT
This is curious.  The file should exist and we have tests that verify that it does.  I believe there may be an issue but I'll need more information, such as an example project.  What is the size of the file?  Is there anything interesting about the network conditions or the disk of the device?
Comment 3 Garrigan Stafford 2022-09-29 13:46:21 PDT
Created attachment 462716 [details]
Sample Project
Comment 4 Garrigan Stafford 2022-09-29 13:48:46 PDT
Interesting. It has been behaving on iOS with the file not existing consistently. I have attached a sample project that when run on an iOS device will attempt to download a file and show whether or not it exists at the supplied path during and after the delegate call.

As for download type and size, I have tested with a variety of sizes ranging form <1MB to ~25MB. I don't have the list immediately on me as it has been a while since testing. If i find it I will add to the comments.
Comment 5 Garrigan Stafford 2022-09-29 13:49:34 PDT
Also for the network conditions or disk there isn't anything that I would think of. it has reproduced on multiple models of iPhone and iPad and the networks are just normal home networks.
Comment 6 Garrigan Stafford 2023-05-31 09:12:38 PDT
Hey has there been any updates to this issue?
Comment 7 Garrigan Stafford 2023-10-17 10:16:39 PDT
Have y'all been able to reproduce with the sample project?