英文:
Downloading From Parse on Apple TV
问题
我已设置了一个解析服务器,并在其中存储了小型的PDF文件(425KB)。我需要我的Apple TV 能够显示这些文件,但由于它们经常更改,所以必须从解析服务器获取,而不仅仅是来自应用程序更新的主包。我遇到的问题是在Apple TV 上缺少NSDocumentsDirectory。你们是如何处理这个问题的呢?我一直在使用缓存目录,但使用当前的代码似乎只有一半的时间能正常工作。如果我在AppDelegate中的启动时运行它,当需要PDF时,它可能不在那里,如果我将其设置为在需要时立即运行此代码,会有延迟,有时根本不显示。使用NSTemporaryDirectory()
是否更好呢?更新,不,它不行。在模拟器上运行正常,在Apple TV上,必须运行两次代码才能下载并显示PDF。
-(void)sermonTime {
//检查PFFile是否存在,如果存在则显示PDF,否则显示空白时间。
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];
}];
}
}
英文:
I have a parse server set up, and as part of it, small PDFs (425KB) are stored on it. I need my Apple TV to be able to display these, but since they change often, it has to come from Parse server, and not just the main bundle where I update it with each update of the app. The issue I'm running into is the lack of an NSDocumentsDirectory on the Apple TV. How do y'all handle this? I've been using the Cache directory, but it seems to only work half the time with the code I am currently using. If I run it at launch from AppDelegate, by the time the PDF is needed, it may not be there, and if I have it set to run this code right when I need it, there is a delay, and sometimes, it simply doesn't show up. Would using NSTemporaryDirectory()
be better? UPDATE, no, it doesn't. Works fine on simulator, on Apple TV, have to run the code two times to get it to both download, and draw the 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];
}];
}
}
答案1
得分: 3
-(void)sermonTime {
// 检查是否存在PFFile,如果存在,则显示PDF,如果不存在,留空时间。
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]) {
// 使用PDF的缓存副本
NSURL *url = [NSURL fileURLWithPath:pdfPath];
self.view.backgroundColor = [UIColor blackColor];
self.arrayOfVerses = @[@"allverses"];
CGPDFDocumentRef pdfDocument = [self openPDF:url];
[self drawDocument:pdfDocument];
} else {
// 下载并保存PDF
[thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (error) {
// 处理错误
NSLog(@"Error downloading PDF: %@", error);
[self blankTime];
} else {
[imageData writeToFile:pdfPath atomically:YES];
// 使用完成块来表示PDF已准备好显示
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不显示。
英文:
-(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];
});
}
}];
}
}
}
Made some changes to the code.
It will first check if the PDF exists cache, it will use the PDF if it exists in cache and will only proceed download if it does not exists. Then, to make sure that PDF is downloaded and saved successfully you can use a completion block. With completion block, it will only proceed to draw it when the block is called to avoid the PDF does't show up.
答案2
得分: 0
我认为您由于缓存问题而出现了问题。一种解决方案是在新的PDF可用时清除缓存。您可以在下载新文件之前通过删除缓存的PDF文件来实现这一点。
LE:以下是一个使用writeToFile:atomically:并调用完成块的修订版本。
- (void)sermonTime {
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]) {
// 使用缓存的PDF副本
NSURL *url = [NSURL fileURLWithPath:pdfPath];
self.view.backgroundColor = [UIColor blackColor];
self.arrayOfVerses = @[@"allverses"];
CGPDFDocumentRef pdfDocument = [self openPDF:url];
[self drawDocument:pdfDocument];
} else {
// 在下载新文件之前删除缓存的PDF文件
[fileManager removeItemAtPath:pdfPath error:nil];
// 下载并保存PDF
[thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (error) {
NSLog(@"下载PDF时出错:%@", error);
[self blankTime];
} else {
[imageData writeToFile:pdfPath atomically:YES];
// 使用完成块来表示PDF已准备好显示
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];
});
}
}];
}
}
}
请注意,这是Objective-C代码,用于处理缓存和下载PDF文件的逻辑。
英文:
I think you have problems because of the cache. A solution could be to clear the cache whenever a new PDF becomes available. You can do this by deleting the cached PDF file before downloading the new one.
LE: Here's a revised version that uses writeToFile:atomically: and then calls the completion block
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
- (void)sermonTime {
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 {
// Delete the cached PDF file before downloading the new one
[fileManager removeItemAtPath:pdfPath error:nil];
// Download and save the PDF
[thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (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];
});
}
}];
}
}
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论