iOS Facebook 的 completionHandler 块有问题
Trouble with iOS Facebook's completionHandler block
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/friendlists"
parameters:@{ @"fields": @"name",}
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
jsonArr1 = [result objectForKey:@"data"];
//Inside the block
for(int i=0; i<5; ++i)
NSLog(@"%@", [[jsonArr1 objectAtIndex:i]objectForKey:@"name"]); //works fine
}]
//Outside the block
for(int i=0; i<5; ++i)
NSLog(@"%@", [[jsonArr1 objectAtIndex:i]objectForKey:@"name"]); //gives out (null)
我发现块是异步调用的,这意味着仅在执行外部 for 循环后才调用块,这就是为什么它在块外给出 null 而在块内输出正常。
是否有任何解决方法可以在块外使用 jsonArr1
?我想在 UITableView 的 tableView:cellForRowAtIndexPath
方法中使用该数组。
有人可以帮忙吗? iOS.
网上没有最新的Fb整合教程
异步执行的全部要点是你调用一个带有完成处理程序的方法,方法 returns 立即安排真正的工作在其他队列而不是被调用的队列上完成,并且一旦方法的工作完成并且结果已准备好供您处理,回调处理程序将被调用 。
因此,在您的完成处理程序中处理数据。如果您需要更新 UI,请执行以下操作:
[someFaceThingy doSomethingHere:@"/path/user/bobdobbs" completionHandler:^(NSDictionary *results, NSError *error ){
if (!results) { .... process error and return ....}
... process results ...
dispatch_async(dispatch_get_main_queue(), ^{
... apply results to table view ...
});
}];
... don't process anything here because there'll be nothing to process ...
Is this correct?
I did the way just you said but still it isn't working out!
-(void) viewDidLoad
{
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(aQueue, ^{
jsonArr = [result objectForKey:@"data"];
for(int i=0; i<[jsonArr count]; ++i)
{
[ultimateArr addObject:[[jsonArr objectAtIndex:i]objectForKey:@"name"]];
//Don't know why but objects are not being added to the ultimateArr
NSLog(@"%@", [[jsonArr objectAtIndex:i]objectForKey:@"name"]);
}
dispatch_async(dispatch_get_main_queue(), ^{
[myTableView reloadData];
});
});
}];
}
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/friendlists"
parameters:@{ @"fields": @"name",}
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
jsonArr1 = [result objectForKey:@"data"];
//Inside the block
for(int i=0; i<5; ++i)
NSLog(@"%@", [[jsonArr1 objectAtIndex:i]objectForKey:@"name"]); //works fine
}]
//Outside the block
for(int i=0; i<5; ++i)
NSLog(@"%@", [[jsonArr1 objectAtIndex:i]objectForKey:@"name"]); //gives out (null)
我发现块是异步调用的,这意味着仅在执行外部 for 循环后才调用块,这就是为什么它在块外给出 null 而在块内输出正常。
是否有任何解决方法可以在块外使用 jsonArr1
?我想在 UITableView 的 tableView:cellForRowAtIndexPath
方法中使用该数组。
有人可以帮忙吗? iOS.
网上没有最新的Fb整合教程异步执行的全部要点是你调用一个带有完成处理程序的方法,方法 returns 立即安排真正的工作在其他队列而不是被调用的队列上完成,并且一旦方法的工作完成并且结果已准备好供您处理,回调处理程序将被调用 。
因此,在您的完成处理程序中处理数据。如果您需要更新 UI,请执行以下操作:
[someFaceThingy doSomethingHere:@"/path/user/bobdobbs" completionHandler:^(NSDictionary *results, NSError *error ){
if (!results) { .... process error and return ....}
... process results ...
dispatch_async(dispatch_get_main_queue(), ^{
... apply results to table view ...
});
}];
... don't process anything here because there'll be nothing to process ...
Is this correct?
I did the way just you said but still it isn't working out!
-(void) viewDidLoad
{
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(aQueue, ^{
jsonArr = [result objectForKey:@"data"];
for(int i=0; i<[jsonArr count]; ++i)
{
[ultimateArr addObject:[[jsonArr objectAtIndex:i]objectForKey:@"name"]];
//Don't know why but objects are not being added to the ultimateArr
NSLog(@"%@", [[jsonArr objectAtIndex:i]objectForKey:@"name"]);
}
dispatch_async(dispatch_get_main_queue(), ^{
[myTableView reloadData];
});
});
}];
}