我正在使用Objective-C中的iPhone应用程序(使用
Xcode 6.1.1和Parse),我刚刚得到了这个神秘的NSInternalInconsistencyException:
捕获“NSInternalInconsistencyException”,原因是“尝试使用指向未保存的新对象的指针保存对象.”:
所以我对Stack Overflow社区的问题是:
有人如何读取此堆栈跟踪以查找问题的实际来源?我没有在此堆栈跟踪中的任何位置看到任何可识别的文件名,方法调用或行号.
或者,如果不是简单地读取堆栈跟踪并且涉及其他技术,那么开发人员应该采取哪些适当的下一步来追踪此类错误的来源?
这是输出到我的控制台的完整堆栈跟踪:
2015-07-18 02:01:17.596 testapp[1276:60b] [Error]: Caught "NSInternalInconsistencyException" with reason "Tried to save an object with a pointer to a new,unsaved object.": ( 0 CoreFoundation 0x2f547f9b + 154 1 libobjc.A.dylib 0x39c94ccf objc_exception_throw + 38 2 CoreFoundation 0x2f547ec5 + 0 3 testapp 0x00205a29 -[PFObject(Private) resolveLocalId] + 384 4 testapp 0x00233d6d __32-[PFrestcommand resolveLocalIds]_block_invoke + 24 5 testapp 0x00233783 +[PFrestcommand forEachLocalIdIn:doblock:] + 642 6 testapp 0x00233ba7 __42+[PFrestcommand forEachLocalIdIn:doblock:]_block_invoke + 62 7 CoreFoundation 0x2f484043 + 98 8 CoreFoundation 0x2f483f67 + 162 9 testapp 0x0023367f +[PFrestcommand forEachLocalIdIn:doblock:] + 382 10 testapp 0x00233ba7 __42+[PFrestcommand forEachLocalIdIn:doblock:]_block_invoke + 62 11 CoreFoundation 0x2f484043 + 98 12 CoreFoundation 0x2f483f67 + 162 13 testapp 0x0023367f +[PFrestcommand forEachLocalIdIn:doblock:] + 382 14 testapp 0x0023373f +[PFrestcommand forEachLocalIdIn:doblock:] + 574 15 testapp 0x00233ba7 __42+[PFrestcommand forEachLocalIdIn:doblock:]_block_invoke + 62 16 CoreFoundation 0x2f484043 + 98 17 CoreFoundation 0x2f483f67 + 162 18 testapp 0x0023367f +[PFrestcommand forEachLocalIdIn:doblock:] + 382 19 testapp 0x00233ca3 -[PFrestcommand forEachLocalId:] + 162 20 testapp 0x00233d3f -[PFrestcommand resolveLocalIds] + 34 21 testapp 0x0023ee2f -[PFrestcommandRunner _runcommandAsync:withCancellationToken:] + 110 22 testapp 0x0023e8c7 -[PFrestcommandRunner runcommandAsync:withOptions:cancellationToken:] + 174 23 testapp 0x0023e7d7 -[PFrestcommandRunner runcommandInBackground:inoperation:] + 42 24 testapp 0x00203667 __65+[PFObject(Private) _deepSaveAsync:withCurrentUser:sessionToken:]_block_invoke_3 + 766 25 testapp 0x002854b3 __55-[BFTask continueWithExecutor:block:cancellationToken:]_block_invoke_2 + 214 26 libdispatch.dylib 0x3a17c833 + 10 27 libdispatch.dylib 0x3a183ad7 + 222 28 libdispatch.dylib 0x3a183d29 + 56 29 libsystem_pthread.dylib 0x3a2bebd3 _pthread_wqthread + 298 30 libsystem_pthread.dylib 0x3a2bea98 start_wqthread + 8 ).
我感谢您提供给我的任何帮助和见解.
解决方法
这个特殊的例子并没有让开发人员继续下去.因此,在这种情况下,您的问题的答案是“Google it”.真的,堆栈跟踪的最佳线索是:
[PFObject(Private) _deepSaveAsync:withCurrentUser:sessionToken:]
上面的行表示使用currentUser的保存失败,并结合错误消息“尝试使用指向新未保存对象的指针保存对象”.应足以指出可能的原因.
事实证明,这个错误并不罕见,并且令人困惑的是为什么Parse没有解决它.这通常是由您的应用程序使用匿名Parse用户并且在用户对象保存之前尝试保存对象引起的.由于尚未保存用户对象,因此它没有objectId,另一个对象的保存失败.
解决方案是检查currentUser对象是否具有objectId,如果没有,则在尝试写入Parse之前先保存它.
// Prevent race condition for unsaved user // Courtesy of: http://samwize.com/2014/07/15/pitfall-with-using-anonymous-user-in-parse/ if ([PFUser currentUser].objectId == nil) { [[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded,NSError *error) { //do stuff later }]; } else { //do stuff Now }