iOS核心数据dispatch_async后台排队崩溃

时间:2016-04-07 02:22:06

标签: ios core-data grand-central-dispatch fault nscache

我得到了不可重现的崩溃,我不知道为什么。我在后台队列上缓存图像。图像名称是Core Data NSManagedObject子类CCCard上的属性。与此同时,我有一个集合视图,也可以访问这些CCCard。

以下是相关代码和注意事项。

//CCDeckViewController.m --------------------------------------

- (void)viewDidLoad {
    // This will fetch the CCCard objects from Core Data.
    self.cards = [[CCDataManager shared] cards];

    [self cacheImages];
}

- (void)cacheCardImages {
    // Because these are NSManagedObjects, I access them in the background
    // via their object ID. So pull the IDs out here.
    NSArray *cardIds = [self.cards valueForKey:@"objectID"];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        for (NSManagedObjectID *cardId in cardIds) {

            // Fetch the actual CCCard object.
            CCCard *card = (CCCard *)[[CCDataManager shared] objectWithId:cardId];

            // Cache the card's image if it's not already there.
            NSString *key = [card thumbnailCacheKey];
            if ([self.cardImageCache objectForKey:key]) {
                continue;
            }
            CCDiscardableImage *discardable = [CCHelper decompressedImageForPath:key size:[card thumbnailSize] tintable:[card tintable]];
            [self.cardImageCache setObject:discardable forKey:key];
        }
    });
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CCCard *card = self.cards[indexPath.item];

    // This line calls the code that crashes.
    UIColor *color = [card color];

    // More code that returns the cell.
}


// CCCard.m --------------------------------------

- (UIColor *)color {
    // Crash happens on this line.
    if (self.colorId.integerValue == 0) {
        // some code
    }
}

以下是我认为最相关的Crashlytics报告的堆栈跟踪部分:

Thread #0: Crashed: com.apple.main-thread
EXC_BREAKPOINT 0x000000000000defe
0  CoreData                       0x24c1b070 _sharedIMPL_pvfk_core + 247
1  CoreData                       0x24c1b071 _sharedIMPL_pvfk_core + 248
2  myapp                          0x4286f -[CCCard color] (CCCard.m:37)
3  myapp                          0x40bbb -[CCDeckViewController collectionView:cellForItemAtIndexPath:] (CCDeckViewController.m:127)

Thread #4: com.apple.root.utility-qos
0  libsystem_pthread.dylib        0x2321920c RWLOCK_GETSEQ_ADDR
1  libsystem_pthread.dylib        0x23219295 pthread_rwlock_unlock + 60
2  libobjc.A.dylib                0x22cb8e01 rwlock_tt<false>::unlockRead() + 8
3  libobjc.A.dylib                0x22cb5af5 lookUpImpOrForward + 488
4  libobjc.A.dylib                0x22cb5903 _class_lookupMethodAndLoadCache3 + 34
5  libobjc.A.dylib                0x22cbbd7b _objc_msgSend_uncached + 26
6  CoreData                       0x24c1d3ab _PFObjectIDFastEquals64 + 38
7  CoreFoundation                 0x233eeed4 CFBasicHashFindBucket + 1820
8  CoreFoundation                 0x233ee775 CFDictionaryGetValue + 116
9  CoreData                       0x24c17037 _PFCMT_GetValue + 122
10 CoreData                       0x24c16ec7 -[NSManagedObjectContext(_NSInternalAdditions) _retainedObjectWithID:optionalHandler:withInlineStorage:] + 58
11 CoreData                       0x24c1b45d _PF_FulfillDeferredFault + 940
12 CoreData                       0x24c1afcf _sharedIMPL_pvfk_core + 86
13 myapp                          0x42991 -[CCCard imagePath] (CCCard.m:53)
14 myapp                          0x41d5b __39-[CCDeckViewController cacheCardImages]_block_invoke (CCDeckViewController.m:295)

令人沮丧的是因为它在开发过程中从未发生过,因此无法测试任何理论。我试图理解代码和崩溃报告,以便现在解决问题。我的猜测是它与故障有关,因为它访问CCCard对象的实例方法,但在尝试从中读取属性时崩溃。但为什么?

1 个答案:

答案 0 :(得分:4)

你违反了线程限制。

根据Core Data文档,只能在分配给它的队列中访问与其关联的NSManagedObjectContext和任何NSManagedObject

您的dispatch_async违反了该规则,需要更正。您不再需要在dispatch_async中使用核心数据的设计。

如果您希望处理是异步的,那么您应该生成一个私有NSManagedObjectContext,它是您的主NSManagedObjectContext的子项,然后针对它执行-performBlock:。这将为您提供正确配置的后台处理。

此外,我强烈建议启用-com.apple.CoreData.ConcurrencyDebug 1,因为这会在开发过程中发现这样的问题。