簡體   English   中英

具有Parse的IOS中的后台線程

[英]Background thread in IOS with Parse

我正在構建一個使用Parse作為Web后端的應用程序。 我正在使用以下代碼,但運行時它告訴我:

*** 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

我的解析由這些列組成,當前只有一行包含此數據:

  • objectId:UYpXIiQbPQ
  • createdAt:2014-09-26T17:00:28.079Z
  • 更新時間:2014-09-26T17:01:19.128Z
  • ACL :(未定義)
  • 故事:“我在星期一早上7點醒來。那只狗叫着,天空開了”(只是完全隨機的測試)
  • 作者:Erik
  • 評論(數組):(未定義)
  • 類別(字符串):測試類別

有人可以幫忙嗎?

----編輯----堆棧跟蹤:

    (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

非常感謝! 此致Erik

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

可能是沒有完成塊而運行PFQuery的結果。 解析提供了一些選項,例如countObjectsInBackground

*由於未捕獲的異常'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]);

}
}

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM