macos – PDFDocument removePageAtIndex:更新到Mac OS X 10.11时不起作用

《macos – PDFDocument removePageAtIndex:更新到Mac OS X 10.11时不起作用》
《macos – PDFDocument removePageAtIndex:更新到Mac OS X 10.11时不起作用》

我正在将PDFKit框架用于PDFViewer的cocoa应用程序.当我试图从PDFDocument中删除其中一个页面时该应用程序在代码行冻结

[[self pdfDocument]  removePageAtIndex:0]; // can see this Problem only in Mac OS X 10.11

当我在Mac OS X 10.10中运行应用程序时,这非常有效

我阅读了所有相关的苹果文档,但我还没有得到任何解决方案.

这是回溯:

* thread #1: tid = 0x85e1, 0x00007fff92571f5e libsystem_kernel.dylib`__psynch_cvwait + 10, queue = 'com.apple.main-thread', stop reason = signal SIGTERM
    frame #0: 0x00007fff92571f5e libsystem_kernel.dylib`__psynch_cvwait + 10
    frame #1: 0x00000001006c05f7 libsystem_pthread.dylib`_pthread_cond_wait + 767
    frame #2: 0x00007fff904c6e32 Foundation`-[__NSOperationInternal _waitUntilFinished:] + 131
    frame #3: 0x00007fff904921fa Foundation`-[NSOperationQueue waitUntilAllOperationsAreFinished] + 254
  * frame #4: 0x000000010017efe1 Neat`-[NRMPDFCoordinator waitUntilAllOperationsAreFinished](self=0x0000608000ad3be0, _cmd=0x00007fff8877e285) + 145 at NRMPDFCoordinator.m:1362
    frame #5: 0x00000001000109cf Neat`-[NRMItemEditorDocument saveDocumentWithDelegate:didSaveSelector:contextInfo:](self=0x000
[[self pdfDocument]  removePageAtIndex:0]; // can see this Problem only in Mac OS X 10.11

0094a190, _cmd=0x00007fff88777581, delegate=0x0000000000000000, didSaveSelector=0x0000000000000000, contextInfo=0x0000000000000000) + 1151 at NRMItemEditorDocument.m:325
frame #6: 0x000000010001018a Neat`-[NRMItemEditorDocument saveDocument:](self=0x000

[[self pdfDocument]  removePageAtIndex:0]; // can see this Problem only in Mac OS X 10.11

0094a190, _cmd=0x00007fff8874cbb4, sender=0x00006080003a8b20) + 58 at NRMItemEditorDocument.m:234
frame #7: 0x0000000100013bef Neat`-[NRMItemEditorWindowController saveAndClose:](self=0x00006080003a8b20, _cmd=0x00000001002cf2d2, sender=0x000

[[self pdfDocument]  removePageAtIndex:0]; // can see this Problem only in Mac OS X 10.11

0094a5b0) + 95 at NRMItemEditorWindowController.m:244
frame #8: 0x00007fff87068082 libsystem_trace.dylib`_os_activity_initiate + 75
frame #9: 0x00007fff87fc79b5 AppKit`-[NSApplication sendAction:to:from:] + 460
frame #10: 0x00007fff87fd9bb2 AppKit`-[NSControl sendAction:to:] + 86
frame #11: 0x00007fff87fd9adc AppKit`__26-[NSCell _sendActionFrom:]_block_invoke + 131
frame #12: 0x00007fff87068082 libsystem_trace.dylib`_os_activity_initiate + 75
frame #13: 0x00007fff87fd9a39 AppKit`-[NSCell _sendActionFrom:] + 144
frame #14: 0x00007fff87068082 libsystem_trace.dylib`_os_activity_initiate + 75
frame #15: 0x00007fff87fd805e AppKit`-[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2693
frame #16: 0x00007fff88020d1c AppKit`-[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 744
frame #17: 0x00007fff87fd6788 AppKit`-[NSControl mouseDown:] + 669
frame #18: 0x00007fff88524575 AppKit`-[NSWindow _handleMouseDownEvent:isDelayedEvent:] + 6322
frame #19: 0x00007fff88525559 AppKit`-[NSWindow _reallySendEvent:isDelayedEvent:] + 212
frame #20: 0x00007fff87f6ad31 AppKit`-[NSWindow sendEvent:] + 517
frame #21: 0x00007fff87eeaccb AppKit`-[NSApplication sendEvent:] + 2540
frame #22: 0x0000000100225f35 Neat`-[NRMApplication sendEvent:](self=0x000

[[self pdfDocument]  removePageAtIndex:0]; // can see this Problem only in Mac OS X 10.11

001205a0, _cmd=0x00007fff88749e04, event=0x0000608000725640) + 1141 at NRMApplication.m:95
frame #23: 0x00007fff87d51f3e AppKit`-[NSApplication run] + 796
frame #24: 0x00007fff87d1b162 AppKit`NSApplicationMain + 1176
frame #25: 0x0000000100012d67 Neat`main(argc=3, argv=0x00007fff5fbff718) + 119 at main.m:21
frame #26: 0x0000000100001e74 Neat`start + 52

这是我使用PDFDocument的removePageAtIndex方法的方法

-(NSError *)removePageOpImpl:(NRMPDFOperation *)op
{
    NSLog(@"\n Inside removePageOpImpl Method ...");
    NSError* error = [self loadDocument];
    if( !error )
    {
        NSUInteger index = [self pageIndexForId:[op pageId]];
        NSLog(@"Page count: %ld", [self pageCount]);
        if( index < [self pageCount] )
        {
            NSLog(@"PDF Document:-- %@", [self pdfDocument]);
            NSLog(@"Index is: %ld", index);
            @try {

                [(PDFDocument *)[self pdfDocument]  removePageAtIndex:index];//At this line the app getting freezed and control is ended.

                NSLog(@"Page count after delete: %ld", [self pageCount]);


            }
            @catch (NSException *exception) {
                NSLog(@"Exception: %@", exception);
            }
            @finally {
                NSLog(@"Finally called");
                [[self mutablePageIdList] removeObjectAtIndex:index];
                [self updatePageLabelsFromIndex:index];
                [self updateChangeCount:NSChangeDone];
                self.contentsChanged = YES;
            }
        }
        else
        {
            // TODO: error
        }
    }
    return error;
}

任何人都可以建议我可能是什么问题…还附加阻止UI的队列截图

我尝试将主队列上的dispatch_async应用于PDFDocument页面删除操作,如下所示

- (NSError *)removePageOpImpl:(NRMPDFOperation *)op
{
    NSError* error = [self loadDocument];
    if( !error )
    {
        NSUInteger index = [self pageIndexForId:[op pageId]];
        if( index < [self pageCount] )
        {
                dispatch_async(dispatch_get_main_queue(), ^{
                        [[self pdfDocument] removePageAtIndex:index];
                        [[self mutablePageIdList] removeObjectAtIndex:index];
                        [self updatePageLabelsFromIndex:index];
                        [self updateChangeCount:NSChangeDone];
                        self.contentsChanged = YES;

                        });
        }
        else
        {
            // TODO: error
        }
    }
    return error;
}

现在应用程序没有挂起,但我陷入了另一个问题.我有其他操作应该在removePageOpImpl操作后同步运行.但它们在removePageOpImpl完成之前执行,这正在改变我的应用程序的行为.你能否告诉我如何在removePageOpImpl之后同步执行其他操作.我读到了关于完成处理程序但是,在这种情况下,我对如何使用它感到困惑.

请建议

最佳答案 你在这里创造了一个漂亮的小僵局.

您的主线程正在等待操作完成,但该操作(由此跟踪指示)…

《macos – PDFDocument removePageAtIndex:更新到Mac OS X 10.11时不起作用》

正在运行在后台线程上,正如您所看到的,它正在等待最有可能只从主线程发出信号的信号量.这有一些类似dispatch_sync的主要线程的标记.由于它发生在PDFDocument实现中,我想它正在尝试确保它在返回给用户之前在主线程上运行某些东西(读/写锁

因此,您的主线程正在等待操作完成,并且该操作正在等待主线程完成它正在尝试执行的操作.经典的僵局.

发生这种情况是因为主线程启动了保存,然后在进行之前等待所有内容完成(但其他一些工作也需要在主线程上运行).

您需要将保存作为异步操作启动,因此在等待操作完成时它不会在主线程上运行.然后,当保存完成时,如果您必须更新UI,它可以通过在主线程上运行的完成块或委托来报告其成功/失败.

点赞