executeFetchRequest挂起,永远不会返回错误或继续

时间:2013-08-31 03:46:57

标签: ios core-data nsfetchrequest

我正在使用AppDelegate的默认managedObjectContext,当我executeFetchRequest时,它永远不会转到下一行或因错误而崩溃。这是在后台线程,因此UI不会冻结,但数据永远不会填充 - 我的活动指示器永远不会停止,我的调试告诉我它永远不会脱离该声明。我逐步完成了每一行,然后将其缩小到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的调用都在同一个线程上吗?如果这是解决方案,我该怎么做?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题并检查了这个帖子: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];
        ...

    }
}

它似乎已经解决了我的问题。