iOS在线搜索数据库时遇到NSRangeException崩溃

问题描述:

我收到偶尔崩溃当我尝试使用UISearchBar/searchDisplayController搜索某些文本时。我正在通过JSON从在线数据库中搜索数据,并且99%的时间内一切正常。我正在使用textDidChange委托方法在其搜索中搜索用户类型。iOS在线搜索数据库时遇到NSRangeException崩溃

这里是崩溃我得到的一个例子:

Fatal Exception: NSRangeException 
-[__NSCFArray objectAtIndex:]: index (2) beyond bounds (2) 

Fatal Exception: NSRangeException 
*** -[__NSArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 0] 

这真的很难让我明确了什么是给我的崩溃,因为当我尝试重新什么造成了事故,我不能。该应用程序可以很好地工作几天,并进行大量“测试”(例如,我一遍又一遍地输入查询)。然后随机会崩溃,然后几天没有。

以下是一些更新我的TableViews的委托方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return [[SearchInfo sharedInfo].searchArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

////////Adds search data to local Array to pass it around the applicatio////// 
searchData = [[SearchInfo sharedInfo].searchArray objectAtIndex:indexPath.row]; 
static NSString *cellIdentifier = @"Cell"; 

//Standard Table View Cell 
UITableViewCell *searchCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (searchCell==nil) { 

searchCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
} 


///////////Search bar tableview////////// 
if (tableView == self.searchDisplayController.searchResultsTableView) { 
searchCell.textLabel.text = [SearchData objectForKey:@"title"]; 
searchCell.detailTextLabel.text = [SearchData objectForKey:@"system_title"]; 
} 

///////Builds the custom tableview cell////// 
UILabel *searchTitleLabel = (UILabel*)[searchCell viewWithTag:1]; 
searchTitleLabel.text = [searchData objectForKey:@"title"]; 
UILabel *systemTitleLabel = (UILabel *)[searchCell viewWithTag:2]; 
systemTitleLabel.text = [searchData objectForKey:@"system_title"]; 
UILabel *devLabel = (UILabel*)[searchDataCell viewWithTag:3]; 

if([[searchData objectForKey:@"developer"] isKindOfClass:[NSString class]]) 

{ 
if([[searchData objectForKey:@"developer"] isEqualToString:@""] || [[searchData objectForKey:@"developer"] isEqualToString:@"<null>"]) 
{ 
developerLabel.text = @"Unknown"; 
} 
else { 
developerLabel.text = [NSString stringWithFormat:@"%@",[searchData objectForKey:@"developer"]]; 
} 

} 
else { 
developerLabel.text = @"Unknown"; 
} 



//////////////////////////////////////////////////////////////// 
return searchCell; 
} 

如果您还有其他问题,请告诉我!我在这里拉我的头发。

这里是崩溃日志:

Thread : Fatal Exception: NSRangeException 
0 CoreFoundation     0x2577cc1f __exceptionPreprocess + 126 
1 libobjc.A.dylib    0x32f27c8b objc_exception_throw + 38 
2 CoreFoundation     0x25691a8d CFRunLoopRemoveTimer 
3 Archive.vg      0x00073ddb -[CenterViewController tableView:cellForRowAtIndexPath:] (CenterViewController.m:192) 
4 UIKit       0x28f018c7 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 474 
5 UIKit       0x28f0198b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 54 
6 UIKit       0x28ef70e1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2268 
7 UIKit       0x28d0f2ff -[UITableView layoutSubviews] + 186 
8 UIKit       0x28c394d7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 514 
9 QuartzCore      0x28661a0d -[CALayer layoutSublayers] + 136 
10 QuartzCore      0x2865d3e5 CA::Layer::layout_if_needed(CA::Transaction*) + 360 
11 QuartzCore      0x2865d26d CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16 
12 QuartzCore      0x2865cc51 CA::Context::commit_transaction(CA::Transaction*) + 224 
13 QuartzCore      0x2865ca55 CA::Transaction::commit() + 324 
14 QuartzCore      0x2865692d CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 56 
15 CoreFoundation     0x257433b5 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20 
16 CoreFoundation     0x25740a73 __CFRunLoopDoObservers + 278 
17 CoreFoundation     0x25740e7b __CFRunLoopRun + 914 
18 CoreFoundation     0x2568f211 CFRunLoopRunSpecific + 476 
19 CoreFoundation     0x2568f023 CFRunLoopRunInMode + 106 
20 GraphicsServices    0x2ca880a9 GSEventRunModal + 136 
21 UIKit       0x28c9b1d1 UIApplicationMain + 1440 
22 Archive.vg      0x0009d0cd main (main.m:16) 
23 libdyld.dylib     0x334a7aaf start + 2 
+0

没关系所有的代码。只需显示崩溃发生的代码_,请。 – matt 2014-10-28 20:49:53

+0

'[SearchInfo sharedInfo] .searchArray'正在发生明显变化。您应该将其锁定,以便在对'numberOfRowsInSection:'和'cellForRowAtIndexPath:'的调用之间不发生变化。 – 2014-10-28 20:51:34

+0

@matt我添加了一个崩溃日志。它与SIGABRT崩溃。当我回家时,我会添加一个异常断点。 – DookieMan 2014-10-28 20:57:20

可以为例外设置断点。它在调试

真正的帮助,您可以启用它菜单上调试 - > Breakpoints->创建异常断点

接下来你打的异常时,你会看到它从何而来。

补充:

@synchronized([SearchInfo sharedInfo].searchArray) { ... update table here ... }

+0

当您点击断点时,您可以看到自己的位置,但不会显示错误消息,直到您继续(Control-Command-Y)两次。断点位于打印错误消息之前。 – zaph 2014-10-28 20:57:08

+0

尝试锁定或@synchronize [SearchInfo sharedInfo] .searchArray以防止表更新自己时更改它? – ekiyanov 2014-10-28 21:02:34

+0

@ekiyanov就像@synchronized(self){[SearchInfo sharedInfo] .searchArray}一样简单吗? – DookieMan 2014-10-28 21:06:21

你说的没错,这变化的用户输入文本。这是问题的根源吗?

是的。在加载表格时,您需要保持数据的一致性。如果您想懒惰,只需执行indexPath.row < [[SearchInfo sharedInfo].searchArray count]检查并返回空单元格。这不是最好的解决方案。实际上,你不应该经常改变你的数据;如果您确实需要更改数据,请为每个数据集创建一个新的UITableView,并静态分配其数据源以确保它们独立加载数据。

+0

好吧,我会给我一个镜头,当我回家。谢谢! – DookieMan 2014-10-28 21:07:21