本文介绍了iOS崩溃'NSInternalInconsistencyException',reason:'语句仍然活动'Core Data缓存相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 NSFetchedResultsController 在屏幕上的崩溃报告中偶尔看到这些弹出窗口,并且不知道如何解决它们。我不相信我在任何地方使用线程,除非 NSFetchedResults 在内部使用它们。



'NSInternalInconsistencyException',reason:'语句仍然是活动'是我得到的完整解释。



两个最近的堆栈跟踪:

  0 CoreFoundation 0x37a368bf __exceptionPreprocess + 163 
1 libobjc.A.dylib 0x3151c1e5 objc_exception_throw + 33
2 CoreData 0x340b2ea5 - [NSSQLiteStatement cachedSQLiteStatement] + 1
3 CoreData 0x340b274f - [NSSQLiteConnection prepareSQLStatement:] + 55
4 CoreData 0x34156049 - [NSSQLChannel selectRowsWithCachedStatement:] + 61
5 CoreData 0x34181d63 newFetchedRowsForFetchPlan_MT + 783
6 CoreData 0x340bab07 - NSSQLCore newRowsForFetchPlan:] + 351
CoreData 0x34160011 - [NSSQLCore fetchRowForObjectID:] + 1005
CoreData 0x340cca57 - [NSSQLCore newValuesForObjectWithID:withContext:error:] + 195
9 CoreData 0x340cbf83 _PFFaultHandlerLookupRow + 423
10 CoreData 0x340cba97 _PF_FulfillDeferredFault + 187
11 CoreData 0x340cb94f _sharedIMPL_pvfk_core + 39
12 PowerPro 0x0006a779 - [GuestCard getPrimaryProspectiveTenant](GuestCard.m:77)
13 PowerPro 0x00017bf9 - [OutstandingFollowupsViewController configureCell :atIndexPath:](OutstandingFollowupsViewController.m:208)
14 PowerPro 0x00017b9b - [OutstandingFollowupsViewController tableView:cellForRowAtIndexPath:](OutstandingFollowupsViewController.m:203)

最后异常回溯:
0 CoreFoundation 0x37a368bf __exceptionPreprocess + 163
1 libobjc.A.dylib 0x3151c1e5 objc_exception_throw + 33
2 CoreData 0x340b2ea5 - [NSSQLiteStatement cachedSQLiteStatement] + 1
3 CoreData 0x340b274f - [NSSQLiteConnection prepareSQLStatement:] + 55
4 CoreData 0x34156049 - [NSSQLChannel selectRowsWithCachedStatement:] + 61
5 CoreData 0x340c26eb - [NSSQLCore _newRowsForFetchPlan:selectedBy:withArgument:] + 515
6 CoreData 0x340bab3f - [NSSQLCore newRowsForFetchPlan:] + 407
7 CoreData 0x3415ea55 - [NSSQLCore newFetchedPKsForSourceID:andRelationship:] + 2217
8 CoreData 0x3416a935 - [NSSQLCore newValueForRelationship:forObjectWithID:withContext:error:] + 689
9 CoreData 0x34108f8b - [NSFaultHandler retainFulfillAggregateFaultForObject:andRelationship:withContext :] + 479
10 CoreData 0x340dcb23 - [_ NSFaultingMutableSet willRead] + 219
11 CoreData 0x340dc70b - [_ NSFaultingMutableSet count] + 23
12 PowerPro 0x00016eb1 - [BrowseGuestCardsViewController configureCell:atIndexPath:](BrowseGuestCardsViewController。 m:246)
13 PowerPro 0x00017173 - [BrowseGuestCardsViewController tableView:cellForRowAtIndexPath:](BrowseGuestCardsViewController.m:222)
14 UIKit 0x34e4e9cb - [UITableView(UITableViewInternal)_createPreparedCellForGlobalRow:withIndexPath:] + 547
15 UIKit 0x34e4daa9 - [UITableView(_UITableViewPrivate)_updateVisibleCellsNow:] + 1077
16 UIKit 0x34e4d233 - [UITableView layoutSubviews] + 207
17 UIKit 0x34df1d4b - [UIView(CALayerDelegate)layoutSublayersOfLayer:] + 183
18 CoreFoundation 0x3799522b - [NSObject performSelector:withObject:] + 43
19 QuartzCore 0x3318c381 - [CALayer layoutSublayers] + 217
20 QuartzCore 0x3318bf99 CA :: Layer :: layout_if_needed(CA :: Transaction *)+ 217
21 QuartzCore 0x3318bea5 - [CALayer layoutIfNeeded] + 153
22 UIKit 0x34eb6fe1 - [UIButton titleLabel] + 73
23 PowerPro 0x00017983 - [BrowseGuestCardsViewController viewDidLoad](BrowseGuestCardsViewController.m:75)


解决方案

很可能这是一个线程问题,另一个。在问题中没有足够的信息可以确定,但是值得检查崩溃日志,并查看异常时所有线程的堆栈跟踪,并查看是否有任何其他线程正在做任何互动与受管对象。它可能是你有一个通知处理程序,一个URL连接完成处理程序或一些其他位使用并发的代码,你没有注意到,在后台使用核心数据对象。值得注意的是,在多个线程上使用的上下文中,对任何托管对象设置或访问任何属性都可能触发此类异常,这使得诊断变得非常困难。 p>

在大多数应用程序中解决这些问题的相对简单的解决方案是将该代码的执行推送到主线程(使用 dispatch_async())。或者,如果你有很多处理,最好在后台线程上创建一个子上下文,并从子上下文中重新获取对象,然后保存子上下文。当然,要重新获取对象,你需要有它的 objectID ,这是一个属性,只能在线程上访问的对象最初提取...


Very occasionally seeing these pop up in crash reports on screens using NSFetchedResultsController, and not sure how to address them. I don't believe I'm using threading anywhere, unless NSFetchedResults is using them internally.

'NSInternalInconsistencyException', reason: 'statement is still active' is the full explanation I get.

Two recent stack traces:

0   CoreFoundation                      0x37a368bf __exceptionPreprocess + 163
1   libobjc.A.dylib                     0x3151c1e5 objc_exception_throw + 33
2   CoreData                            0x340b2ea5 -[NSSQLiteStatement cachedSQLiteStatement] + 1
3   CoreData                            0x340b274f -[NSSQLiteConnection prepareSQLStatement:] + 55
4   CoreData                            0x34156049 -[NSSQLChannel selectRowsWithCachedStatement:] + 61
5   CoreData                            0x34181d63 newFetchedRowsForFetchPlan_MT + 783
6   CoreData                            0x340bab07 -[NSSQLCore newRowsForFetchPlan:] + 351
7   CoreData                            0x34160011 -[NSSQLCore fetchRowForObjectID:] + 1005
8   CoreData                            0x340cca57 -[NSSQLCore newValuesForObjectWithID:withContext:error:] + 195
9   CoreData                            0x340cbf83 _PFFaultHandlerLookupRow + 423
10  CoreData                            0x340cba97 _PF_FulfillDeferredFault + 187
11  CoreData                            0x340cb94f _sharedIMPL_pvfk_core + 39
12  PowerPro                            0x0006a779 -[GuestCard getPrimaryProspectiveTenant] (GuestCard.m:77)
13  PowerPro                            0x00017bf9 -[OutstandingFollowupsViewController configureCell:atIndexPath:] (OutstandingFollowupsViewController.m:208)
14  PowerPro                            0x00017b9b -[OutstandingFollowupsViewController tableView:cellForRowAtIndexPath:] (OutstandingFollowupsViewController.m:203)

Last Exception Backtrace:
0   CoreFoundation                      0x37a368bf __exceptionPreprocess + 163
1   libobjc.A.dylib                     0x3151c1e5 objc_exception_throw + 33
2   CoreData                            0x340b2ea5 -[NSSQLiteStatement cachedSQLiteStatement] + 1
3   CoreData                            0x340b274f -[NSSQLiteConnection prepareSQLStatement:] + 55
4   CoreData                            0x34156049 -[NSSQLChannel selectRowsWithCachedStatement:] + 61
5   CoreData                            0x340c26eb -[NSSQLCore _newRowsForFetchPlan:selectedBy:withArgument:] + 515
6   CoreData                            0x340bab3f -[NSSQLCore newRowsForFetchPlan:] + 407
7   CoreData                            0x3415ea55 -[NSSQLCore newFetchedPKsForSourceID:andRelationship:] + 2217
8   CoreData                            0x3416a935 -[NSSQLCore newValueForRelationship:forObjectWithID:withContext:error:] + 689
9   CoreData                            0x34108f8b -[NSFaultHandler retainedFulfillAggregateFaultForObject:andRelationship:withContext:] + 479
10  CoreData                            0x340dcb23 -[_NSFaultingMutableSet willRead] + 219
11  CoreData                            0x340dc70b -[_NSFaultingMutableSet count] + 23
12  PowerPro                            0x00016eb1 -[BrowseGuestCardsViewController configureCell:atIndexPath:] (BrowseGuestCardsViewController.m:246)
13  PowerPro                            0x00017173 -[BrowseGuestCardsViewController tableView:cellForRowAtIndexPath:] (BrowseGuestCardsViewController.m:222)
14  UIKit                               0x34e4e9cb -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 547
15  UIKit                               0x34e4daa9 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1077
16  UIKit                               0x34e4d233 -[UITableView layoutSubviews] + 207
17  UIKit                               0x34df1d4b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 183
18  CoreFoundation                      0x3799522b -[NSObject performSelector:withObject:] + 43
19  QuartzCore                          0x3318c381 -[CALayer layoutSublayers] + 217
20  QuartzCore                          0x3318bf99 CA::Layer::layout_if_needed(CA::Transaction*) + 217
21  QuartzCore                          0x3318bea5 -[CALayer layoutIfNeeded] + 153
22  UIKit                               0x34eb6fe1 -[UIButton titleLabel] + 73
23  PowerPro                            0x00017983 -[BrowseGuestCardsViewController viewDidLoad] (BrowseGuestCardsViewController.m:75)
解决方案

It's more than likely that this is a threading problem in some way or another. There isn't quite enough information in the question to see for sure, but it's worth check through the crash logs and looking at the stack traces for all threads at the time of the exception, and see if any other threads are doing anything which interacts with a managed object. It could be that you have a notification handler, a URL connection completion handler, or some other bit of code using concurrency which you hadn't noticed, using core data objects in the background. It's worth noting that setting or accessing any property on any managed object from a context that's being used on more than one thread could potentially trigger this kind of exception, which can make it pretty tough to diagnose.

The relatively simple solution to solving these problems in most apps is to push the execution of that code to the main thread (using dispatch_async()). Alternatively, if you've got lots of processing to do it might be better to create a child context on the background thread and re-fetch the object from the child context, then save the child context. Of course, to re-fetch the object you need to have its objectID, which is a property which can only be accessed on the thread the object was originally fetched on…

这篇关于iOS崩溃'NSInternalInconsistencyException',reason:'语句仍然活动'Core Data缓存相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:11