将项添加到块内的NSMutableArray中会出现问题

时间:2012-12-10 16:28:34

标签: objective-c ios nsmutablearray objective-c-blocks

更新: 我刚刚发现了一个棘手的部分...如果我做这样的事情,它的确有效:

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

但事实并非如此:

[_friendsArrayInBlock insertObject:friend atIndex:itemIndex];

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

原始部分: 在这里您可以看到相关的代码部分:

@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。

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

2 个答案:

答案 0 :(得分:3)

Blocks可以在不使用__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];
        }
    }];
}

答案 1 :(得分:-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];
}