Facebook请求线程问题 [英] Facebook request thread problem

查看:145
本文介绍了Facebook请求线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我是一个新的线程,所以我有这个问题。
我试图从Facebook获取朋友的信息,我不想在主线程上这样做。但是由于某种原因,当请求不在主线程上时,回调永远不会被调用,我不知道为什么!



我有一个Array,所有的ID从我的朋友和循环遍历这个数组,并创建一个对象,我的自定义类Friend(它获取所需的所有信息)与每个ID。
我将此对象添加到数组。
这个朋友对象向Facebook提出请求并处理响应以获取我想要的数据。



这里是代码:

  dispatch_async(dispatch_get_global_queue(0,0),^(void){
[self getFBFriendsInfo];
});


- (void)getFBFriendsInfo {

if(friendsInfoArray){
[friendsInfoArray removeAllObjects];
}
else {
friendsInfoArray = [[NSMutableArray alloc] init];




for(int i = 0; i< [UDID count]; i ++){
NSString * udid = [UDID objectAtIndex:一世];
FriendsInfo * friend = [[FriendsInfo alloc] initWithFacebook:facebook andUdid:udid];
[friendsInfoArray addObject:friend];
[朋友发行];

}

dispatch_async(dispatch_get_main_queue(),^(void){
[delegate friendsInfosAvailable:friendsInfoArray];
});

}



和我的自定义类我这样做:

  [facebook requestWithGraphPath:udid andDelegate:self]; 

与此回调不会被调用!只有当我在主线程上执行请求时,它才起作用:

  dispatch_async(dispatch_get_main_queue(),^(void){
[facebook requestWithGraphPath:udid andDelegate:self];


});


解决方案

这就是为什么在不同的线程上你没有回应:



Facebook将使用NSURLConnection执行请求。要使连接正常工作,调用线程的运行循环必须以默认运行循环模式运行(读NSURLConnection类引用)。当您使用dispatch_async()时,没有运行循环在默认运行循环模式下运行(除非您在主调度队列上,因此在主线程上运行)。因此,我认为请求甚至没有发送(您可以检查嗅探您的网络,如果你愿意)。



所以,简而言之,你应该让你的请求主线程;因为它是异步的,它不会冻结你的应用程序。那么,如果响应的处理过于昂贵,请在后台处理。



我真的希望这有帮助。



我最好的。


Alright I am kind of new to threads so I have this question. I am trying to get information of friends from Facebook, and I do not want to do that on the main thread. but for some reason when the request is not on the main thread the callback does never get called and I don't know why!

I have an Array with all the ID's from my friends and loop through this array and create an object of my custom class Friend (which gets all the information I need) with every ID. I add this object to an array. This friend object makes an request to Facebook and handles the response to get the data I want.

here is the code:

dispatch_async(dispatch_get_global_queue(0, 0), ^(void) {
            [self getFBFriendsInfo];
        }); 


 -(void)getFBFriendsInfo{

if (friendsInfoArray) {
    [friendsInfoArray removeAllObjects];
}
else{
    friendsInfoArray =[[NSMutableArray alloc]init];
}



for (int i=0; i<[UDID count]; i++) {
    NSString *udid = [UDID objectAtIndex:i];
    FriendsInfo *friend =[[FriendsInfo alloc] initWithFacebook:facebook andUdid:udid];
    [friendsInfoArray addObject:friend];
    [friend release];

}

dispatch_async(dispatch_get_main_queue(), ^(void) {
    [delegate friendsInfosAvailable:friendsInfoArray];
});

}

and in my custom class I do this:

[facebook requestWithGraphPath:udid andDelegate:self];

with this the callback's are never called! only if I do the request on the main thread it works:

dispatch_async(dispatch_get_main_queue(), ^(void) {
    [facebook requestWithGraphPath:udid andDelegate:self];


});

解决方案

This is why on a different thread you get no response:

Facebook will use NSURLConnection to perform requests. For the connection to work correctly the calling thread’s run loop must be operating in the default run loop mode (Read NSURLConnection class reference). When you use dispatch_async() there is no run loop operating in the default run loop mode (unless you are on the main dispatch queue, therefore running on the main thread). Hence, I figure the request isn't even sent (You can check that sniffing your network if you wish.).

So, in a nutshell, you should make your request on the main thread; as it is asynchronous, it wont freeze your app. Then, if the processing of the response is too expensive, handle it in the background.

I really hope this helps.

My best.

这篇关于Facebook请求线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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