【发布时间】:2012-05-21 09:00:53
【问题描述】:
我有一个包含名为 currentBusiness 的变量的单例
我像这样连接 currentBusiness 属性
-(Business *) currentBusiness
{
if ([[NSThread currentThread] isMainThread])
{
DLog(@"I am a main thread");
}
else
{
assert (false);
DLog(@"I am not a main thread");
}
CLog(@"_currentBusiness %@ withObjectID: %@", _currentBusiness, _currentBusiness.objectID);
if (IsEmpty(_currentBusiness))
{
//assert(false);
}
if (_currentBusiness.isFault)
{
assert(false); //This is reached
}
return _currentBusiness;
}
另外,我放了一个类别让业务陷阱会转错
- (void)willTurnIntoFault
{
if ([[NSThread currentThread] isMainThread])
{
CLog(@"Faulting: %@", self); //called sometimes and during time of interest never called
if (self == [BNUtilitiesQuick currentBusiness])
{
CLog(@"Current Business is Faulting");//never called
}
}
[super willTurnIntoFault];
}
所以,起初一切都很好。正如预期的那样,[BNUtilities currentBusiness] 为视图控制器提供了必要的信息。但是,在随机(尽管几乎可以肯定)位置,如果我改变视图,说我想激活呼叫(使用简单的代码)。突然
if (_currentBusiness.isFault)
{
assert(false); //This is reached
}
据我所知,绝对没有任何事情会改变 _currentBusiness。基础业务不会被删除。
我也放了绊线
-(void) setCurrentBusiness:(Business *)currentBusiness
{
if ([[NSThread currentThread] isMainThread])
{
DLog(@"I am a main thread");
}
else
{
assert (false);
DLog(@"I am not a main thread");
}
_currentBusiness = currentBusiness;
}
并注意它没有到达。所以 _currentBusiness,突然就变成了故障
这是 _currentBusiness 的“正常”内容
2012-05-21 15:42:24.952 BadgerNew[2292:17003] <0x87909a0 UtilitiesQuick.m:(69)> _currentBusiness <Business: 0x96a8980> (entity: Business; id: 0x9683b30 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Business/p15> ; data: {
Aliases = "<relationship fault: 0x96b8540 'Aliases'>";
Bookmark = 0;
Building = "0x9683b30 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Business/p15>";
City = "0x9694880 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/City/p2>";
Distance = "689.0068307560858";
DistanceGrouping = "0x96b9b90 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/DistanceGrouping/p890>";
Districts = (
"0x92a6450 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/District/p4>"
);
Email = nil;
ID = "universitas-indonusa-esa-unggul__-6.19_106.78";
Images = (
"0x93edb50 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Image/p20>"
);
InBuildingAddress = nil;
LatitudeLongitude = "0x96bd6a0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/LatitudeLongitude/p15>";
Like = 0;
OpeningHour = nil;
PeopleCount = 295;
Phones = (
"0x92dcbf0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Phone/p4>"
);
Price = 0;
Promotions = "<relationship fault: 0x9693480 'Promotions'>";
Rating = "0x96a0f00 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Rating/p15>";
RatingGroup = "0x9682bd0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/RatingGroup/p15>";
Reviews = (
);
Street = "Jl. Arjuna Utara";
Tags = (
"0x92b5c10 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Tag/p12>"
);
Tenants = "<relationship fault: 0x96e6fd0 'Tenants'>";
TimeStamp = nil;
Title = "Universitas Indonusa Esa Unggul";
URLs = "<relationship fault: 0x968d930 'URLs'>";
Website = nil;
Zip = 11510;
checkIn = nil;
pinAndLineNumber = 2;
timeLike = nil;
updated = 1;
})
当它出现故障并中断时
if (_currentBusiness.isFault)
{
assert(false); //This is reached
}
这是结果:
2012-05-21 15:42:36.782 BadgerNew[2292:17003] <0x87909a0 UtilitiesQuick.m:(69)> _currentBusiness <Business: 0x96a8980> (entity: Business; id: 0x9683b30 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Business/p15> ; data: <fault>)
所以是的,我做一些 po 和
(lldb) po [_currentBusiness objectID]
error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its type is unknown
warning: couldn't get object pointer (substituting NULL): Couldn't load '_cmd' because its type is unknown
Execution was interrupted, reason: Attempted to dereference an invalid pointer..
The process has been returned to the state before execution.
就好像 _currentBusiness 的内容变了一样。 current_Business 仍然存在。它仍然具有相同的地址。
这到底是怎么回事?
不,这不仅仅是错误。出不来是个毛病。
所以我添加了更多的绊线
-(Business *) currentBusiness
{
if ([[NSThread currentThread] isMainThread])
{
DLog(@"I am a main thread");
}
else
{
assert (false);
DLog(@"I am not a main thread");
}
CLog(@"_currentBusiness %@ withObjectID: %@", _currentBusiness, _currentBusiness.objectID); //add ObjectID
if (IsEmpty(_currentBusiness))
{
//assert(false);
}
if (_currentBusiness.isFault)
{
CLog(@"_currentBusiness.Title %@", _currentBusiness.Title); //Try to unfault
assert(false);
}
return _currentBusiness;
}
我敢打赌我不能无错。
结果是这样的:
2012-05-21 16:08:37.950 BadgerNew[2625:17003] <0x9510470 UtilitiesQuick.m:(69)> _currentBusiness <Business: 0x9589410> (entity: Business; id: 0x95889d0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Business/p15> ; data: {
Aliases = "<relationship fault: 0x953cdf0 'Aliases'>";
Bookmark = 0;
Building = "0x95889d0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Business/p15>";
City = "0x958dde0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/City/p2>";
Distance = "689.0068307560858";
DistanceGrouping = "0x9582f40 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/DistanceGrouping/p924>";
Districts = (
"0x874b0d0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/District/p4>"
);
Email = nil;
ID = "universitas-indonusa-esa-unggul__-6.19_106.78";
Images = (
"0x95c4be0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Image/p20>"
);
InBuildingAddress = nil;
LatitudeLongitude = "0x958f430 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/LatitudeLongitude/p15>";
Like = 0;
OpeningHour = nil;
PeopleCount = 295;
Phones = (
"0x9237680 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Phone/p4>"
);
Price = 0;
Promotions = "<relationship fault: 0x954bae0 'Promotions'>";
Rating = "0x9584b50 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Rating/p15>";
RatingGroup = "0x9586970 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/RatingGroup/p15>";
Reviews = (
);
Street = "Jl. Arjuna Utara";
Tags = (
"0x8769120 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Tag/p12>"
);
Tenants = "<relationship fault: 0x955fd90 'Tenants'>";
TimeStamp = nil;
Title = "Universitas Indonusa Esa Unggul";
URLs = "<relationship fault: 0x955f620 'URLs'>";
Website = nil;
Zip = 11510;
checkIn = nil;
pinAndLineNumber = 2;
timeLike = nil;
updated = 1;
}) withObjectID: 0x95889d0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Business/p15>
然后
2012-05-21 16:08:42.797 BadgerNew[2625:17003] <0x9510470 UtilitiesQuick.m:(69)> _currentBusiness <Business: 0x9589410> (entity: Business; id: 0x95889d0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Business/p15> ; data: <fault>) withObjectID: 0x95889d0 <x-coredata://3993C454-9EE9-4EDC-B1FD-7073A9CEC194/Business/p15>
2012-05-21 16:08:42.797 BadgerNew[2625:17003] <0x9510470 UtilitiesQuick.m:(77)> _currentBusiness.Title (null)
(lldb) po [_currentBusiness objectID]
error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its type is unknown
warning: couldn't get object pointer (substituting NULL): Couldn't load '_cmd' because its type is unknown
Execution was interrupted, reason: Attempted to dereference an invalid pointer..
The process has been returned to the state before execution.
所以 _currentBusiness 的地址不会改变。 objectID 不会改变。不知何故,它变成了错误。也调用 _currentBusiness.Title 导致 (null)
这似乎在 ARC 更新之前不会发生,尽管我不确定是否是 ARC 引起的。
【问题讨论】:
标签: objective-c automatic-ref-counting xcode4.3 fault