我使用的是AppDelegate的默认managedObjectContext,当我在其上使用executeFetchRequest时,它永远不会移至下一行或因错误而崩溃。它在后台线程上,因此UI不会冻结,但是数据永远不会填充-我的 Activity 指示器永远不会停止,调试会告诉我它永远不会退出该语句。我逐步完成每一行,并将其范围缩小到executeFetchRequest方法。是什么原因造成的?没有保存任何对象,但我认为这与它无关。到目前为止,已经一个小时了,所以显然有什么地方出错了。最后,这只发生了40%的时间。

代码如下所示:

NSSortDescriptor *byTimestamp = [NSSortDescriptorWithKey:@"timestamp" ascending:NO];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Message"];
[fetchRequest setSortDescriptors:@[byTimestamp]];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSArray *messages = [managedObjectContext executeFetchRequest:fetchRequest error:&error];//hangs on this line!!
if(error != nil)
  NSLog(@"Error: %@", [error localizedDescription]);

这是我的回溯:
* thread #1: tid = 0x1c03, 0x9329d7d2 libsystem_kernel.dylib`mach_msg_trap + 10, stop reason = signal SIGSTOP
    frame #0: 0x9329d7d2 libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x9329ccb0 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x02488a49 CoreFoundation`__CFRunLoopServiceMachPort + 185
    frame #3: 0x0248d84b CoreFoundation`__CFRunLoopRun + 1243
    frame #4: 0x0248cf44 CoreFoundation`CFRunLoopRunSpecific + 276
    frame #5: 0x0248ce1b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x029637e3 GraphicsServices`GSEventRunModal + 88
    frame #7: 0x02963668 GraphicsServices`GSEventRun + 104
    frame #8: 0x0041165c UIKit`UIApplicationMain + 1211
    frame #9: 0x000027ad qub`main(argc=1, argv=0xbffff418) + 141 at main.m:16

  thread #3: tid = 0x2203, 0x932a09ca libsystem_kernel.dylib`kevent64 + 10
    frame #0: 0x932a09ca libsystem_kernel.dylib`kevent64 + 10
    frame #1: 0x01c8cc2b libdispatch.dylib`_dispatch_mgr_invoke + 863
    frame #2: 0x01c8c8cc libdispatch.dylib`_dispatch_mgr_thread + 61

  thread #4: tid = 0x2303, 0x932a00ee libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #0: 0x932a00ee libsystem_kernel.dylib`__workq_kernreturn + 10
    frame #1: 0x9017904c libsystem_c.dylib`_pthread_workq_return + 45
    frame #2: 0x90178e19 libsystem_c.dylib`_pthread_wqthread + 448

  thread #5: tid = 0x2403, 0x9329d7d2 libsystem_kernel.dylib`mach_msg_trap + 10
    frame #0: 0x9329d7d2 libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x9329ccb0 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x02488a49 CoreFoundation`__CFRunLoopServiceMachPort + 185
    frame #3: 0x0248d8d4 CoreFoundation`__CFRunLoopRun + 1380
    frame #4: 0x0248cf44 CoreFoundation`CFRunLoopRunSpecific + 276
    frame #5: 0x0248ce1b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x055ad310 WebCore`RunWebThread(void*) + 608
    frame #7: 0x90176557 libsystem_c.dylib`_pthread_start + 344

  thread #8: tid = 0x2703, 0x9329d7d2 libsystem_kernel.dylib`mach_msg_trap + 10
    frame #0: 0x9329d7d2 libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x9329ccb0 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x02488a49 CoreFoundation`__CFRunLoopServiceMachPort + 185
    frame #3: 0x0248d8d4 CoreFoundation`__CFRunLoopRun + 1380
    frame #4: 0x0248cf44 CoreFoundation`CFRunLoopRunSpecific + 276
    frame #5: 0x0248ce1b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x00ec7c7c Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 298
    frame #7: 0x00ec7b45 Foundation`-[NSRunLoop(NSRunLoop) run] + 82
    frame #8: 0x00085c60 qub`+[AFURLConnectionOperation networkRequestThreadEntryPoint:](self=0x000d2f1c, _cmd=0x000af418, object=0x00000000) + 304 at AFURLConnectionOperation.m:195
    frame #9: 0x00f150d5 Foundation`-[NSThread main] + 76
    frame #10: 0x00f15034 Foundation`__NSThread__main__ + 1304
    frame #11: 0x90176557 libsystem_c.dylib`_pthread_start + 344

  thread #9: tid = 0x2803, 0x9329d7d2 libsystem_kernel.dylib`mach_msg_trap + 10
    frame #0: 0x9329d7d2 libsystem_kernel.dylib`mach_msg_trap + 10
    frame #1: 0x9329ccb0 libsystem_kernel.dylib`mach_msg + 68
    frame #2: 0x02488a49 CoreFoundation`__CFRunLoopServiceMachPort + 185
    frame #3: 0x0248d8d4 CoreFoundation`__CFRunLoopRun + 1380
    frame #4: 0x0248cf44 CoreFoundation`CFRunLoopRunSpecific + 276
    frame #5: 0x0248ce1b CoreFoundation`CFRunLoopRunInMode + 123
    frame #6: 0x00fccebe Foundation`+[NSURLConnection(Loader) _resourceLoadLoop:] + 393
    frame #7: 0x00f150d5 Foundation`-[NSThread main] + 76
    frame #8: 0x00f15034 Foundation`__NSThread__main__ + 1304
    frame #9: 0x90176557 libsystem_c.dylib`_pthread_start + 344

  thread #10: tid = 0x2903, 0x9329fbe6 libsystem_kernel.dylib`select$DARWIN_EXTSN + 10
    frame #0: 0x9329fbe6 libsystem_kernel.dylib`select$DARWIN_EXTSN + 10
    frame #1: 0x024cacb7 CoreFoundation`__CFSocketManager + 1255
    frame #2: 0x90176557 libsystem_c.dylib`_pthread_start + 344

  thread #11: tid = 0x2a03, 0x9329f91a libsystem_kernel.dylib`__psynch_mutexwait + 10
    frame #0: 0x9329f91a libsystem_kernel.dylib`__psynch_mutexwait + 10
    frame #1: 0x9017c13b libsystem_c.dylib`pthread_mutex_lock + 595
    frame #2: 0x014cdb01 CoreData`-[_PFLock lock] + 33
    frame #3: 0x014cdada CoreData`-[NSPersistentStoreCoordinator lock] + 42
    frame #4: 0x014e257e CoreData`-[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1182
    frame #5: 0x014e09c9 CoreData`-[NSManagedObjectContext executeFetchRequest:error:] + 569
    frame #6: 0x000492fe qub`__36-[RIPConvosViewController pullData:]_block_invoke(.block_descriptor=0x118e9320, newMessages=0x00000000, oldMessages=0x00000000) + 638 at RIPConvosViewController.m:73
    frame #7: 0x00048fb4 qub`-[RIPConvosViewController pullData:](self=0x075293d0, _cmd=0x000abda7, completion=0xb0375ef0) + 212 at RIPConvosViewController.m:103
    frame #8: 0x0004bab8 qub`__40-[RIPConvosViewController refreshTable:]_block_invoke(.block_descriptor=0x118d2e50) + 168 at RIPConvosViewController.m:236
    frame #9: 0x01c8753f libdispatch.dylib`_dispatch_call_block_and_release + 15
    frame #10: 0x01c99014 libdispatch.dylib`_dispatch_client_callout + 14
    frame #11: 0x01c8a2e8 libdispatch.dylib`_dispatch_root_queue_drain + 335
    frame #12: 0x01c8a450 libdispatch.dylib`_dispatch_worker_thread2 + 39
    frame #13: 0x90178e12 libsystem_c.dylib`_pthread_wqthread + 441

顺便说一句,此代码位于RIPConvosViewController类的pullData方法的名为fetchBlock的块中。

我相信这是一个线程问题,但是我不知道如何解决这个问题-我应该确保对Core Data的所有调用都在同一线程上吗?如果这是解决方案,我该怎么办?

最佳答案

我有同样的问题,并检查了这个线程:executeFetchRequest:error: freezes app

我通过将NSManagedObjectContext请求放在@synchronized块中解决了该问题。

我的代码是:

-(void)saveBookmarksToCache:(NSMutableArray *) bookmarkList error:(NSError **)error
{

    NSFetchRequest *allBookmarks = [[NSFetchRequest alloc] init];
    [allBookmarks setEntity:self.bookmarkDescription];
    [allBookmarks setIncludesPropertyValues:NO]; // Just fetch the Id

    __autoreleasing NSError *reqError = nil;
    NSArray *bookmarks = [self.managedObjectContext executeFetchRequest:allBookmarks error:&reqError];


    if (reqError != nil)
        error = &reqError;
    ...

    // Save
    [self.managedObjectContext save:&reqError];
    ...
}

并且它一直挂在executeFetchRequest行上(我有几个类或多或少同时执行相同类型的过程)。

正如另一个线程所建议的那样,我将其转换为:
-(void)saveBookmarksToCache:(NSMutableArray *) bookmarkList error:(NSError **)error
{

    NSFetchRequest *allBookmarks = [[NSFetchRequest alloc] init];
    [allBookmarks setEntity:self.bookmarkDescription];
    [allBookmarks setIncludesPropertyValues:NO]; // Just fetch the Id

    __autoreleasing NSError *reqError = nil;
    // Lock the PersistenStoreCoordinator for the whole process
    @synchronized(self.persistentStoreCoordinator) {
        NSArray *bookmarks = [self.managedObjectContext executeFetchRequest:allBookmarks error:&reqError];

        if (reqError != nil)
            error = &reqError;
        ...

        // Save
        [self.managedObjectContext save:&reqError];
        ...

    }
}

它似乎为我解决了这个问题。

关于ios - executeFetchRequest挂起,从不返回错误或继续,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18544278/

10-12 06:31