你能帮我如何获得用户信息吗?
NSString *name;
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
// Success! Include your code to handle the results here
name = [result objectForKey:@"first_name"]; // Error!!! how to get data from this handler
}
else
{
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
}
}];上面描述的代码-异步?如何使其同步?给我讲讲机械装置或者告诉我在哪里读。谢谢!
发布于 2014-06-24 19:37:07
您可以在这个网站上阅读关于Facebook 的所有内容:https://developers.facebook.com。
它们不提供同步API,我甚至不知道您为什么需要它。但如果你真的这么做了,你可以做些解决办法。见执行情况:
__block id result = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id theResult, NSError *error) {
result = theResult;
dispatch_semaphore_signal(semaphore); // UPDATE: dispatch_semaphore_wait call was here, which is wrong
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"%@", result);// this code will be launched after you've received response from Facebook结果符合FBGraphUser协议。因此,如果它不包含first_name键的值,用户就不会指定它。您可以在调试器中打印结果并查看它是什么。
发布于 2014-06-25 21:18:42
您可以使用其他方法来解决您的问题: 1)将名称设为属性:__block NSString *name 2)将您需要执行的代码移动到单独的方法,如下所示:
- (void) methodWithComplitionHandler
{
name = nil;
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
name = [result objectForKey:@"first_name"];
}
else
{
}
}];
[self futherProcessMethod];
}
- (void) furtherProcessMethod
{
if (self.name == nil)
{
[self performSelector:@selector(furtherProcessMethod) withObject:nil afterDelay:3.0]; // here set appropriate delay
}
else
{
// do some with name;
}
}发布于 2014-06-25 22:12:55
正如我在前面的回答中所阐明的,您需要在“answer”语句中使用name。请调试以获得更多的理解。所以我们有:
- (void) SetUserData
{
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
self.first_name = [result objectForKey:@"first_name"];
}
else
{
}
}];
[self furtherProcessMethod];
}
- (void) furtherProcessMethod
{
if (self.first_name == nil)
{
[self performSelector:@selector(furtherProcessMethod) withObject:nil afterDelay:30.0]; // here set appropriate delay
}
else
{
NSLog(@"first_name: %@", self.first_name);
}
}https://stackoverflow.com/questions/24392164
复制相似问题