我正在使用NSURLRequest加载对象的详细信息。此对象具有“正在加载”的属性。 在ViewController中显示对象的详细信息,该对象有一个观察者,用于' isLoading'。
此观察员将在“loadFromResultList' (在' viewWillAppear'中调用)并且它将在#observe****VerueForKeyPath'中注销。或者在' viewWillDisappear' (如有必要)。
有时,应用程序因“无理由”而崩溃。 (例外类型SIGSEGV)。 所以这意味着观察者消息被发送到一个解除分配的对象。
我尝试使用启用的NSZombies。但这种崩溃有时只会发生。所以我无法真正跟踪它。
以下是代码:
// ViewController
- (void)viewWillAppear:(BOOL)animated {
if (self.advertisement == nil)
{
[self loadAdFromResultList];
}
}
- (void)loadAdFromResultList {
self.advertisement = [appDelegate.currentSearch.result objectAtIndex:self.currentAdIndex];
if ([self.advertisement isKindOfClass:[NSError class]]) {
} else {
[self.advertisement addObserver:self forKeyPath:@"isLoading" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
@try {
[self.advertisement load];
} @catch […]
}
}
// Advertisment Object class
- (void)load {
self.isLoading = true;
[api beginGetAdvertisement:guid target:self selector:@selector(beginGetAdvertisementCallback:) userState:self];
}
// ViewController
- (void)beginGetAdvertisementCallback:(FEURLAsyncResult*)result {
[…]
[api endGetAdvertisement:result];
self.isLoading = false;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"isLoading"]) {
@try {
// Remove observer
[self.advertisement removeObserver:self forKeyPath:@"isLoading"];
}
@catch (NSException *exception) {
NSLog(@"[x] %s: %@", __PRETTY_FUNCTION__, exception);
}
}
}
- (void)viewWillDisappear:(BOOL)animated {
@try {
[self.advertisement removeObserver:self forKeyPath:@"isLoading"];
}
@catch (NSException *exception) {
NSLog(@"err: %@", exception);
}
[super viewWillDisappear:animated];
}
这是Stacktrace:
Thread 0 Crashed:
1 libobjc.A.dylib _objc_msgSend + 6
2 Foundation NSKeyValuePushPendingNotificationPerThread + 215
3 Foundation NSKeyValueWillChange + 475
4 Foundation -[NSObject(NSKeyValueObserverNotification) willChangeValueForKey:] + 181
5 Foundation _NSSetCharValueAndNotify + 77
6 Fewo -[FEAdvertisement load] + 381
7 Fewo -[FEExposeViewController loadAdFromResultList] (FEExposeViewController.m:1145)
8 Fewo -[FEExposeViewController viewWillAppear:] (FEExposeViewController.m:122)
9 UIKit -[UIViewController _setViewAppearState:isAnimating:] + 347
…
14 Fewo -[FEResultListViewController tableView:didSelectRowAtIndexPath:] (FEResultListViewController.m:358)
15 UIKit -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1079
…
26 Fewo main (main.m:17)
任何可能导致此次崩溃的想法都欢迎。
Thx to all