从 Apple TV 上的 Parse 下载

问题描述 投票:0回答:1

我设置了一个解析服务器,作为它的一部分,小型 PDF (425KB) 存储在上面。我需要我的 Apple TV 能够显示这些,但由于它们经常变化,它必须来自 Parse 服务器,而不仅仅是我在每次更新应用程序时更新它的主包。我遇到的问题是 Apple TV 上缺少 NSDocumentsDirectory。你们都是怎么处理的?我一直在使用 Cache 目录,但它似乎只能用我当前使用的代码工作一半的时间。如果我在从 AppDelegate 启动时运行它,到需要 PDF 时,它可能不存在,如果我将它设置为在我需要它时运行此代码,就会有延迟,有时,它根本就没有出现。使用

NSTemporaryDirectory()
会更好吗?更新,不,它没有。在模拟器上工作正常,在 Apple TV 上,必须运行代码两次才能下载和绘制 PDF

-(void)sermonTime {
    //Check if PFFile exists, if so, display PDF, if not, blank time.
    if ([self.entry[@"SermonPresIncluded"] isEqualToString:@"NO"]) {
        [self blankTime];
    }
    else {
        NSLog(@"SermonTime");
        PFFileObject *thumbnail = self.entry[@"SermonPDF"];
       
        [thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[self.entry valueForKey:@"DateOfService"]] stringByAppendingString:@".pdf"];
            
            [imageData writeToFile:pdfPath atomically:YES];
            
            
            NSURL *url = [NSURL fileURLWithPath:pdfPath];
            self.view.backgroundColor = [UIColor blackColor];
            self.arrayOfVerses = @[@"allverses"];
            CGPDFDocumentRef pdfDocument = [self openPDF:url];
            [self drawDocument:pdfDocument];

        }];
    }
      
}
pdf parse-platform tvos nscache pffile
1个回答
2
投票
-(void)sermonTime {
    // Check if PFFile exists, if so, display PDF, if not, blank time.
    if ([self.entry[@"SermonPresIncluded"] isEqualToString:@"NO"]) {
        [self blankTime];
    }
    else {
        NSLog(@"SermonTime");
        PFFileObject *thumbnail = self.entry[@"SermonPDF"];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[self.entry valueForKey:@"DateOfService"]] stringByAppendingString:@".pdf"];

        NSFileManager *fileManager = [NSFileManager defaultManager];

        if ([fileManager fileExistsAtPath:pdfPath]) {
            // Use cached copy of PDF
            NSURL *url = [NSURL fileURLWithPath:pdfPath];
            self.view.backgroundColor = [UIColor blackColor];
            self.arrayOfVerses = @[@"allverses"];
            CGPDFDocumentRef pdfDocument = [self openPDF:url];
            [self drawDocument:pdfDocument];
        } else {
            // Download and save the PDF
            [thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
                if (error) {
                    // Handle the error
                    NSLog(@"Error downloading PDF: %@", error);
                    [self blankTime];
                } else {
                    [imageData writeToFile:pdfPath atomically:YES];

                    // Use completion block to signal that the PDF is ready to display
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NSURL *url = [NSURL fileURLWithPath:pdfPath];
                        self.view.backgroundColor = [UIColor blackColor];
                        self.arrayOfVerses = @[@"allverses"];
                        CGPDFDocumentRef pdfDocument = [self openPDF:url];
                        [self drawDocument:pdfDocument];
                    });
                }
            }];
        }
    }
}

对代码做了一些修改。

它会先检查缓存中是否存在PDF,缓存中存在则使用PDF,不存在才继续下载。然后,为确保成功下载并保存 PDF,您可以使用完成块。使用完成块,它只会在块被调用时继续绘制它以避免 PDF 不显示。

© www.soinside.com 2019 - 2024. All rights reserved.