【发布时间】:2012-12-27 22:18:45
【问题描述】:
我尝试进行 Facebook Graph 调用并将一些项目添加到数组中。这些项目被添加到一个 for 循环中,并且数组看起来很好。但是在 for 循环之后,这些项目就消失了。
我使用带有 Facebook SDK 3.1 的 xcode 4.5。
我错过了什么?
TableViewController.m
#import "TableViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController {
NSMutableArray *recipes;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
recipes = [NSMutableArray arrayWithObjects:@"Egg Benedict", nil];
[recipes addObject:@"foobar"];
NSLog(@"recipes before FBRquestConnection: %@", recipes);
[FBRequestConnection
startWithGraphPath:@"search?type=place¢er=37.785834,-122.406417"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
for (id item in [result objectForKey:@"data"]) {
[recipes addObject:[item objectForKey:@"name"]];
}
NSLog(@"recipes after for-loop: %@", recipes);
NSLog(@"amount of recipes after for-loop: %u", [recipes count]);
}
}];
NSLog(@"amount of recipes after FBRequestConnection: %u", [recipes count]);
}
调试控制台输出
2012-12-27 23:09:44.056 barbar[3481:19a03] recipes before FBRquestConnection: (
"Egg Benedict",
foobar
)
2012-12-27 23:09:44.056 barbar[3481:19a03] amount of recipes after FBRequestConnection: 2
2012-12-27 23:09:44.516 barbar[3481:19a03] recipes after for-loop: (
"Egg Benedict",
foobar,
"Union Square, San Francisco",
"Benefit Cosmetics",
"San Francisco | The Official Guide",
"BareMinerals by Bare Escentuals",
"Viator.com",
"Bleacher Report",
"Downtown San Francisco",
"AMC Metreon 16",
"Cheesecake Factory",
SquareTrade,
"Westfield San Francisco Centre",
"Bloomingdale's San Francisco",
"Macy's San Francisco Union Square",
Xoom,
"Parc 55 Hotel",
"Pottery Barn",
"AT&T Park",
Bebo,
Snapfish,
"Hilton San Francisco Union Square",
uTorrent,
"The Westin St. Francis",
TRUSTe,
"Apple Retail Store - San Francisco"
)
2012-12-27 23:09:44.516 barbar[3481:19a03] amount of recipes after for-loop: 26
干杯——杰里克
【问题讨论】:
-
这里有什么问题,这对我来说很好。您正在执行异步操作,所以日志发生的顺序可能令人困惑?
-
代码运行良好。日志显示了 for 循环之后的所有对象。你为什么不认为这是有效的?请记住,连接块外部(之后)的日志语句在 for 循环实际执行之前被调用。这是因为连接块是在后台线程上完成的。
标签: ios facebook facebook-graph-api nsmutablearray persistent