4

QLPreviewController seems to cache file contents based on the local file's URL. In my application, the file contents can be updated remotely and would cause the new contents to be downloaded.

If I view a file in QLPreviewController, update it remotely, then re-preview it, the file does not show up as updated.

The file is definitely updated on disk, and other controls show the correct updated file.

The workaround I'm using right now is to basically move a file when it's previewed to a unique filename (using timestamp), which will of course not be in the QLPreviewController's cache. However, this has other repercussions, for example, if the app is killed or it crashes (god forbid), I won't know "where" to find the downloaded file.

I'm looking for less invasive hacks, or solutions to making QLPreviewController refresh its cache. The APIs don't seem to expose anything, so don't be afraid to submit a hack if it's less gross than the one I've presented above (not including copying/moving the file to a guaranteed unique URL, which I am already utilizing).

cozykozy
  • 217
  • 1
  • 9

5 Answers5

1

Just ran into this issue myself. I solved it by recreating the QLPreviewController each time I reload an item with the same name as the currently viewed item. Creating a new QLPreviewController clears the cache.

I know this is an old question but someone might have the same problem and find this answer helpful.

tsp
  • 1,938
  • 18
  • 15
  • I did try the same but it doesn't work on every iOS version. In my app, I DO recreate the preview controller every time but cache was not cleared. – Gabriel Apr 08 '13 at 07:59
1

You should use refreshCurrentPreviewItem after downloading complete

Mayank Purwar
  • 265
  • 1
  • 4
  • 16
0

I had the same problem. Opening a locally generated CSV file.

I have my _previewController* setup as a @property of my controller. Then what i did:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.previewController = [[QLPreviewController alloc] init];
    _previewController.delegate=self;
    _previewController.dataSource=self;
}

- (void)previewCSV
{
    [_previewController reloadData]; // this triggers a reload
    [self presentModalViewController:_previewController animated:YES];
}

IN other solution that comes to mind (not tested).

Depending on your URL, you could add something like http://url?time=123456 to your URL. Like this you change the URL but without side effect. The time (or any other parameter) you can change on each request.

Roger
  • 7,535
  • 5
  • 41
  • 63
0

It's the ugliest bug in iOS. Cache management in iOS 5 and beyond. I think is the same reason that makes iCloud buggy, Share-at-Home crashing and so on. Bad cache managements and so worst synchronization systems.

Well, my solution for this was to store the download file in a folder and use the current date to name the folder. It is equivalent to @Rogier's solution, but this works always. You get a name for the folder, for example, with [[NSDate date] description]. Instead of saving the file replacing the old one, you delete previous file, delete previous folder and save new file in a new folder. It's working fine for me.

Gabriel
  • 3,319
  • 1
  • 16
  • 21
0

Just remove all files from tmp directory like this:

- (void)clearCache
{
    NSString *tempPath = NSTemporaryDirectory();
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tempPath error:nil];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    for (int i = 0; i < [dirContents count]; i++) {
        NSLog(@"Directory Count: %i", [dirContents count]);
        NSString *contentsOnly = [NSString stringWithFormat:@"%@%@", tempPath, [dirContents objectAtIndex:i]];
        [fileManager removeItemAtPath:contentsOnly error:nil];
    }
}
obyknovenius
  • 121
  • 1
  • 4