2014-09-27 35 views
0

我正在构建一个使用Parse作为web后端的应用程序。我使用这个代码,但它告诉我这个当我运行它:iOS中的后台线程解析

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 

Warning: A long-running operation is being executed on the main thread. 

我一直在研究,但我不能拿出一个解决方案

我的代码:

页眉:

#import <Parse/Parse.h> 

@interface HomeViewController : PFQueryTableViewController <UITableViewDataSource, UITableViewDelegate> 
@property (weak, nonatomic) IBOutlet UITableView *storiesTableView; 

@end 

实现:

#import "HomeViewController.h" 

@interface HomeViewController() { 
// Declare variables 
NSArray *_stories; 
int itemsCount; 
BOOL firstOpen; 
} 

@end 

@implementation HomeViewController 

- (id)initWithCoder:(NSCoder *)aCoder 
{ 
self = [super initWithCoder:aCoder]; 
if (self) { 

    // The className to query on 
    self.parseClassName = @"Story"; 

    // The key of the PFObject to display in the label of the default cell style 
    self.textKey = @"objectId"; 

    // Whether the built-in pull-to-refresh is enabled 
    self.pullToRefreshEnabled = YES; 

    // Whether the built-in pagination is enabled 
    self.paginationEnabled = NO; 
} 
return self; 
} 

- (PFQuery *)queryForTable 
{ 
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName]; 

    itemsCount = [query countObjects]; 

    return query; 
} 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 

// Wise-verse.. but checks if this is the first open so that the user won 
if (firstOpen == YES) { 
    // Do nothing 
} else { 
    [self performSegueWithIdentifier:@"signInSegue" sender:self]; 
    firstOpen = YES; 
} 
} 

-(void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:YES]; 
// Setup delegate and datasource 
_storiesTableView.dataSource = self; 
_storiesTableView.delegate = self; 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (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 itemsCount; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object 
{ 
static NSString *simpleTableIdentifier = @"cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
} 

// Configure the cell 
UILabel *nameLabel = (UILabel*) [cell viewWithTag:10]; 
nameLabel.text = [object objectForKey:@"objectId"]; 

UILabel *descriptionLabel = (UILabel*) [cell viewWithTag:20]; 
descriptionLabel.text = [object objectForKey:@"Story"]; 

return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
// Segue over to the viewing page 
[self performSegueWithIdentifier:@"detailSegue" sender:self]; 
} 

@end 

我解析包含这些列,并且有电流只有一排有与它这样的数据:

  • 对象ID:UYpXIiQbPQ
  • createdAt:2014-09- 26T17:00:28.079Z
  • updatedAt:2014-09-26T17:01:19.128Z
  • ACL:(undefined)
  • 故事:“我星期一早上7点醒来。那只狗狂吠,天空开放”(只是一个完全随机试验)
  • 作者:埃里克
  • 评论(阵列):(未定义)
  • 类别(字符串):测试分类

有人能帮助这

---- ----编辑 堆栈跟踪:

(lldb) bt 
* thread #1: tid = 0xe3b1, 0x00000001947ac0a8 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 
    frame #0: 0x00000001947ac0a8 libobjc.A.dylib`objc_exception_throw 
    frame #1: 0x00000001840abbec CoreFoundation`-[__NSArrayM objectAtIndex:] + 264 
    * frame #2: 0x000000010012efdc iStory`-[PFQueryTableViewController tableView:cellForRowAtIndexPath:](self=0x000000014c613dc0, _cmd=<unavailable>, otherTableView=<unavailable>, indexPath=<unavailable>) + 196 at PFQueryTableViewController.m:307 
    frame #3: 0x0000000188c3e39c UIKit`-[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 544 
    frame #4: 0x0000000188c32fc4 UIKit`-[UITableView _updateVisibleCellsNow:isRecursive:] + 2360 
    frame #5: 0x0000000188a28c60 UIKit`-[UITableView layoutSubviews] + 172 
    frame #6: 0x0000000188945874 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 572 
    frame #7: 0x000000018829dd58 QuartzCore`-[CALayer layoutSublayers] + 168 
    frame #8: 0x0000000188298944 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 320 
    frame #9: 0x00000001882987e8 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32 
    frame #10: 0x0000000188297fe8 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 276 
    frame #11: 0x0000000188297d6c QuartzCore`CA::Transaction::commit() + 436 
    frame #12: 0x000000018893c848 UIKit`_afterCACommitHandler + 156 
    frame #13: 0x000000018417e388 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32 
    frame #14: 0x000000018417b314 CoreFoundation`__CFRunLoopDoObservers + 360 
    frame #15: 0x000000018417b6f4 CoreFoundation`__CFRunLoopRun + 836 
    frame #16: 0x00000001840a9664 CoreFoundation`CFRunLoopRunSpecific + 396 
    frame #17: 0x000000018d1eb5a4 GraphicsServices`GSEventRunModal + 168 
    frame #18: 0x00000001889ae4f8 UIKit`UIApplicationMain + 1488 
    frame #19: 0x00000001000f541c iStory`main(argc=1, argv=0x000000016fd13a60) + 116 at main.m:14 
    frame #20: 0x0000000194e1aa08 libdyld.dylib`start + 4 

非常感谢! 此致 埃里克

+0

您可以将堆栈跟踪粘贴到显示导致错误的调用序列的问题中吗? (如果你还没有这样做,请在Objective-C异常处设置一个断点。) – 2014-09-27 16:34:31

+0

@PhillipMills对不起 - 但我不完全明白你在说什么 - 如何显示堆栈跟踪以及你想要我在哪里放置断点? – Erik 2014-09-27 16:40:46

+0

如果您转到Xcode中的断点导航器,则可以单击左下角的加号并选择添加异常断点。当抛出异常时,这将停止(默认情况下)。此时,您可以在(lldb)提示符的调试控制台中键入“bt”以查看堆栈。 – 2014-09-27 16:45:27

回答

0

*终止应用程序由于未捕获的异常 'NSRangeException',原因: '* - [__ NSArrayM objectAtIndex:]:索引0超出界限为 空数组'

你的第一个错误说你试图从一个空数组中获得一个值。这可能是因为目前您还没有任何使用价值,因为它尚未完全从Parse下载并可用。尝试使用NSLog itensCount以及queryForTable方法中的Parse对象键值,以检查在使用前是否在您的tableView中接收到任何值。

警告:在主线程 上正在执行长时间运行的操作。

这是因为您正在进行同步联网,这可能会冻结应用程序屏幕,但也可能会使其崩溃。你应该总是做异步联网(使用后台线程块),并且Parse拥有自己的方法,比如findObjectsInBackgroundWithBlock(用于查询)。然后,您可以在实际接收数据之后提供您的tableView。检查解析文档以获取更多信息。 https://www.parse.com/docs/ios_guide#queries/iOS 事情是这样的:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 

// if received successfully 
if (!error) { 

// you have properly received objects so update your tableView with received data 


} 

else 
{ 

// Log details of the failure 
NSLog(@"Parse Error: %@ %@", error, [error userInfo]); 

} 
} 

希望它能帮助。

0
2
Warning: A long-running operation is being executed on the main thread 

可能正在运行的PFQuery没有完成块的结果。解析提供了几个选项,如countObjectsInBackground