NSMutableArray丢失所有对象 [英] NSMutableArray losing all objects

查看:81
本文介绍了NSMutableArray丢失所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的NSMutableArray丢失了我从Facebook搜索中添加的所有数据.我在每次迭代中都遵循了循环,循环正确运行并添加了所有数据,但是一旦执行了此代码,我的NSMutableArrays将恢复为具有零个对象.可以做所有添加都在请求连接中进行的事情吗?数组是在其外部进行贴花和初始化的,所以我不明白为什么会出现问题.任何帮助表示赞赏!

My NSMutableArray is losing all its data that I am adding from a Facebook search. I have followed the loop through each iteration an the loop runs correctly and adds all the data, but once this code is executed, my NSMutableArrays revert to having zero objects. Could it have something to do that all the adding is happening within the request connection? The Arrays are decalred and initialised outside of it though, so I don't understand why it would be the problem. Any help appreciated!

在我的ViewDidLoad

in my ViewDidLoad

_namesArray = [[NSMutableArray alloc] init];
_categoryArray = [[NSMutableArray alloc] init];
[FBRequestConnection startWithGraphPath:@"search?q=food&type=place&center=51.89,-8.472&distance=1000" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    if (!error) {
        NSLog([NSString stringWithFormat:@"user events: %@", result]);
        NSArray *test = [result objectForKey:@"data"];
        for (FBGraphObject *obj in test) {
            if ([obj objectForKey:@"category"]){
                [_namesArray addObject:[obj objectForKey:@"category"]];
            }
            if ([obj objectForKey:@"name"]){
                [_categoryArray addObject:[obj objectForKey:@"name"]];
            }
        }
    } else {
        NSLog([NSString stringWithFormat:@"error %@", error.description]);
    }

}];

头文件

@property (strong, nonatomic) NSMutableArray *namesArray;
@property (strong, nonatomic) NSMutableArray *categoryArray;

推荐答案

数组由completionHandler中的元素填充,这些元素是异步执行的.看来您出现问题的原因是您尝试从completionHandler中访问数组.实际上,您应该使用completionHandler中的数组,或者从此块中启动另一个方法.

Arrays get filled with elements within completionHandler which is executed asynchronously. It seems like the reason of your issue is that you try to access the arrays out of completionHandler. Actually you should work with arrays within completionHandler or start another method from this block.

_namesArray = [[NSMutableArray alloc] init];
_categoryArray = [[NSMutableArray alloc] init];

// 1 - Start request
[FBRequestConnection startWithGraphPath:@"search?q=food&type=place&center=51.89,-8.472&distance=1000" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    // 3 - request is finished. Fill the arrays

    if (!error) {
        NSLog([NSString stringWithFormat:@"user events: %@", result]);
        NSArray *test = [result objectForKey:@"data"];
        for (FBGraphObject *obj in test) {
            if ([obj objectForKey:@"category"]){
                [_namesArray addObject:[obj objectForKey:@"category"]];
            }
            if ([obj objectForKey:@"name"]){
                [_categoryArray addObject:[obj objectForKey:@"name"]];
            }
        }
    } else {
        NSLog([NSString stringWithFormat:@"error %@", error.description]);
    }

    // Use arrays. For example, reload UITableView
    [_tableView reloadData];

}];

// 2 - now request is not finished yet

这篇关于NSMutableArray丢失所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆