0

更新:我刚刚发现了一个棘手的部分......如果我做这样的事情,它会起作用:

[_friendsArrayInBlock insertObject:@"Bla" atIndex:itemIndex];

但它没有:

[_friendsArrayInBlock insertObject:friend atIndex:itemIndex];

为什么我不能添加自定义对象,但我可以添加 NSString?有什么问题

ORIGINAL PART:这里可以看到相关的代码部分:

@implementation ORGFriendsTableViewController
{
    NSMutableArray *_friendsArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _friendsArray = [NSMutableArray array];
    __block NSMutableArray *_friendsArrayInBlock = _friendsArray;

    [FBRequestConnection startForMyFriendsWithCompletionHandler: ^(FBRequestConnection *connection,
                                                                   id result,
                                                                   NSError *error) {
        FBGraphObject *fbGraphObject = (FBGraphObject *)result;

        NSMutableArray *userArray = fbGraphObject[@"data"];
        for (FBGraphObject *user in userArray) {

            ORGFBUser *friend = [[ORGFBUser alloc] initWithId:user[@"id"] name:user[@"name"] andPhotoURL:@"someurl"];

            NSInteger itemIndex = 0;
            [_friendsArrayInBlock insertObject:friend atIndex:itemIndex];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:itemIndex inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
        }
    }];
}

问题出现在这一行:

[_friendsArrayInBlock insertObject:friend atIndex:itemIndex];

程序接收信号“EXC_BAD_ACCESS”

我认为它与块概念有关,并从中改变了非线程安全的 NSMutableArray。

你知道如何解决这个问题吗?

4

2 回答 2

3

块可以在不使用的情况下访问实例变量__block(参见this)。因此这应该工作:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _friendsArray = [NSMutableArray array];

    [FBRequestConnection startForMyFriendsWithCompletionHandler: ^(FBRequestConnection *connection,
                                                                   id result,
                                                                   NSError *error) {
        FBGraphObject *fbGraphObject = (FBGraphObject *)result;

        NSMutableArray *userArray = fbGraphObject[@"data"];
        for (FBGraphObject *user in userArray) {

            ORGFBUser *friend = [[ORGFBUser alloc] initWithId:user[@"id"] name:user[@"name"] andPhotoURL:@"someurl"];

            NSInteger itemIndex = 0;
            [_friendsArray insertObject:friend atIndex:itemIndex];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:itemIndex inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
        }
    }];
}
于 2012-12-10T16:36:31.570 回答
-1

试试这个

@implementation ORGFriendsTableViewController
{
    NSMutableArray *_friendsArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _friendsArray = [NSMutableArray array];
    __block NSMutableArray *_friendsArrayInBlock = _friendsArray;

    [FBRequestConnection startForMyFriendsWithCompletionHandler: ^(FBRequestConnection *connection,
                                                                   id result,
                                                                   NSError *error) {
        FBGraphObject *fbGraphObject = (FBGraphObject *)result;

        NSMutableArray *userArray = fbGraphObject[@"data"];
        for (FBGraphObject *user in userArray) {

            ORGFBUser *friend = [[ORGFBUser alloc] initWithId:user[@"id"] name:user[@"name"] andPhotoURL:@"someurl"];

            [_friendsArrayInBlock addObject:friend];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_friendsArrayInBlock.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
        }
    }];
    [self.tableView reloadData];
}
于 2012-12-10T16:33:24.870 回答