应用程序在 CoreData [NsManagedObjectContext] 中崩溃

App get Crash in the CoreData [NsManagedObjectContext]

应用程序在 MyCrashApp 行崩溃。此崩溃随机发生在应用程序上。我正在尝试使用 CoreData 从服务器加载数据并将它们存储在 table 中,然后在 UIview 上显示。

它在 Crittercism 中显示 SIGSEGV 错误

0   CoreFoundation       0x00000001825edc20 __CFBasicHashDrain + 192
1   CoreData             0x000000018228b1a0 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2092
2   CoreData             0x0000000182289df4 -[NSManagedObjectContext save:] + 164
3   MyCrashApp           0x0000000100109548 0x0000000100090000 + 496968
4   MyApp                0x0000000100104198 0x0000000100090000 + 475544
5   MyApp                0x00000001000dfdf0 0x0000000100090000 + 327152
6   MyApp                0x00000001000eacbc 0x0000000100090000 + 371900
7   libdispatch.dylib    0x00000001978996e8 _dispatch_call_block_and_release + 20
8   libdispatch.dylib    0x00000001978996a8 _dispatch_client_callout + 12
9   libdispatch.dylib    0x00000001978a7b40 _dispatch_root_queue_drain + 2136
10  libdispatch.dylib    0x00000001978a72dc _dispatch_worker_thread3 + 108
11  libsystem_pthread.dylib   0x0000000197aad470 _pthread_wqthread + 1088
12  libsystem_pthread.dylib   0x0000000197aad020 start_wqthread + 0 

这部分代码可能会崩溃

NSError *error;
[[[CoredataHelper sharedInstance] managedObjectContext] save:&error];

我正在尝试存储来自服务器的值

- (void) addArticle:(NSDictionary *) ArticleDict
{
    ArticleData * objArticleData = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([ArticleData class]) inManagedObjectContext:[[CoredataHelper sharedInstance] managedObjectContext]];
    [objArticleData setArticle:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_ARTICLE] != nil && ArticleDict[READ_ARTICLES_ARTICLE] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_ARTICLE]:@""]];
    [objArticleData setArticle_id:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_ID] != nil && ArticleDict[READ_ARTICLES_ID] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_ID]:@""]];
    [objArticleData setArticle_title:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_TITLE] != nil && ArticleDict[READ_ARTICLES_TITLE] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_TITLE]:@""]];
    [objArticleData setArticle_type:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_ARTICLE_TYPE] != nil && ArticleDict[READ_ARTICLES_ARTICLE_TYPE] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_ARTICLE_TYPE]:@""]];
    [objArticleData setArticle_author:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_AUTHOR] != nil && ArticleDict[READ_ARTICLES_AUTHOR] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_AUTHOR]:@""]];
    [objArticleData setArticle_categories:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_CATEGORIES] != nil && ArticleDict[READ_ARTICLES_CATEGORIES] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_CATEGORIES]:@""]];
    [objArticleData setArticle_categoryIds:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_CATEGORY_IDS] != nil && ArticleDict[READ_ARTICLES_CATEGORY_IDS] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_CATEGORY_IDS]:@""]];
    [objArticleData setArticle_countries:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_COUNTRIES] != nil && ArticleDict[READ_ARTICLES_COUNTRIES] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_COUNTRIES]:@""]];
    [objArticleData setArticle_description:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_DESCRIPTION] != nil && ArticleDict[READ_ARTICLES_DESCRIPTION] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_DESCRIPTION]:@""]];
    [objArticleData setArticle_excerpt:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_EXCERPT] != nil && ArticleDict[READ_ARTICLES_EXCERPT] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_EXCERPT]:@""]];
    [objArticleData setArticle_feedmapid:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_FEED_MAP_ID] != nil && ArticleDict[READ_ARTICLES_FEED_MAP_ID] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_FEED_MAP_ID]:@""]];
    [objArticleData setArticle_image:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_IMAGE] != nil && ArticleDict[READ_ARTICLES_IMAGE] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_IMAGE]:@""]];
    [objArticleData setArticle_restImage:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_RESTIMAGE] != nil && ArticleDict[READ_ARTICLES_RESTIMAGE] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_RESTIMAGE]:@""]];
    [objArticleData setArticle_name:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_NAME] != nil && ArticleDict[READ_ARTICLES_NAME] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_NAME]:@""]];
    [objArticleData setArticle_languageId:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_LANGUAGEID] != nil && ArticleDict[READ_ARTICLES_LANGUAGEID] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_LANGUAGEID]:@""]];
    if ((ArticleDict[READ_ARTICLES_NEWS_DATE] != nil && ArticleDict[READ_ARTICLES_NEWS_DATE] != (id)[NSNull null]))
    {
        [objArticleData setArticle_date:[NSString stringWithFormat:@"%@",ArticleDict[READ_ARTICLES_NEWS_DATE]]];
    }
    [objArticleData setArticle_newssecdiff:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_NEWS_SEC_DIFF] != nil && ArticleDict[READ_ARTICLES_NEWS_SEC_DIFF] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_NEWS_SEC_DIFF]:@""]];
    [objArticleData setArticle_news_approved_secdiff:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_NEWS_APPROVED_SEC_DIFF] != nil && ArticleDict[READ_ARTICLES_NEWS_APPROVED_SEC_DIFF] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_NEWS_APPROVED_SEC_DIFF]:@""]];
    [objArticleData setArticle_stateIds:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_STATE_IDS] != nil && ArticleDict[READ_ARTICLES_STATE_IDS] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_STATE_IDS]:@""]];
    [objArticleData setArticle_states:[NSString stringWithFormat:@"%@",(ArticleDict[READ_ARTICLES_STATES] != nil && ArticleDict[READ_ARTICLES_STATES] != (id)[NSNull null])?ArticleDict[READ_ARTICLES_STATES]:@""]];
    NSError *error;
    [[[CoredataHelper sharedInstance] managedObjectContext] save:&error];
}

任何帮助将不胜感激!

您似乎是在后台线程上调用 addArticle(大概是您的网络服务传送数据后的回调)。

您需要在后台执行此操作。修改您的 addArticle 方法,使其也采用用于插入和保存的上下文参数。

NSManagedObjectContext *context = [[NSManagedObjectContext alloc] 
    initWithConcurrencyType: NSPrivateQueueConcurrencyType];
context.parentContext = [[CoredataHelper sharedInstance] managedObjectContext];
[context performBlockAndWait:^{
    [self addArticle: dataDictionary inContext: context]
}];

在此之后,您可能还需要保存主上下文,只需确保在主线程上执行即可,例如通过使用 GCD。