【问题标题】:Parse - warnParseOperationOnMainThread() error thrown when clicking on a pictureParse - 单击图片时引发warnParseOperationOnMainThread() 错误
【发布时间】:2025-11-28 06:05:02
【问题描述】:

基本上按照这个教程在这里https://parse.com/tutorials/saving-images。我到了当用户单击图像时,它会将他们带到新的视图控制器并全屏显示图像。但是,当我单击图片时,它会将我带到黑屏并引发以下错误

Warning: A long-running Parse operation is being executed on the main thread. 
 Break on warnParseOperationOnMainThread() to debug.

我检查了一下,它正在将图片发送到新的视图控制器,因此它可能与 VC 本身有关。我在它要求我的地方放置了一个断点,这就是线程所说的

`My App`warnParseOperationOnMainThread at PFTask.m:15:
0x95fa0:  pushl  %ebp
0x95fa1:  movl   %esp, %ebp
0x95fa3:  subl   $0x8, %esp
0x95fa6:  calll  0x95fab                   ; warnParseOperationOnMainThread + 11 at PFTask.m:15
0x95fab:  popl   %eax
0x95fac:  leal   0x18771d(%eax), %eax
0x95fb2:  movl   %eax, (%esp)
0x95fb5:  calll  0x98d54                   ; symbol stub for: NSLog
0x95fba:  addl   $0x8, %esp
0x95fbd:  popl   %ebp
0x95fbe:  retl  

第 5 个线程指向我的一个类中名为 buttonTouched 的方法。它指向了这行特定的代码:

imageData = [theImage getData];

方法如下:

- (void)buttonTouched:(id)sender {
    // When picture is touched, open a viewcontroller with the image
    PFObject *theObject = (PFObject *)[allImages objectAtIndex:[sender tag]];
    PFFile *theImage = [theObject objectForKey:@"imageFile"];

    NSData *imageData = [[NSData alloc] init];
    imageData = [theImage getData];
    UIImage *selectedPhoto = [UIImage imageWithData:imageData];

    PhotoDetailViewController *pdvc = [[PhotoDetailViewController alloc] init];

    pdvc.selectedImage = selectedPhoto;
    [self presentViewController:pdvc animated:YES completion:nil];
    NSLog(@"Photo controller %@", pdvc.selectedImage);
}

这让我发疯了。一切都与教程几乎相同。还是我需要定义与 2 VC 的 segue 关系?如果这完全相关,我正在使用故事板。

一个很好的解释是如何发生的,一个解决方案会很棒。

【问题讨论】:

    标签: objective-c xcode ios7 parse-platform presentmodalviewcontroller


    【解决方案1】:

    出现警告是因为您正在主线程中下载图像。您可以在后台下载它并在下载完成后呈现视图,如下所示。

    - (void)buttonTouched:(id)sender {
        // When picture is touched, open a viewcontroller with the image
        PFObject *theObject = (PFObject *)[allImages objectAtIndex:[sender tag]];
        PFFile *theImage = [theObject objectForKey:@"imageFile"];
        [theImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            UIImage *selectedPhoto = [UIImage imageWithData:imageData];
    
            PhotoDetailViewController *pdvc = [[PhotoDetailViewController alloc] init];
    
            pdvc.selectedImage = selectedPhoto;
            [self presentViewController:pdvc animated:YES completion:nil];
            NSLog(@"Photo controller %@", pdvc.selectedImage);
        }];
    }
    

    解决此问题的另一种方法可能是不传递 UIImage,而是将 PFObject 传递给呈现的视图,并在加载后下载该视图上的图片。

    【讨论】: