Facebook streaming issue

Good thing I'm new to threads, so I have this question. I'm trying to get friends info from Facebook and I don't want to do this in the main thread. but for some reason, when the request is not in the main thread, callback is never called, and I don’t know why!

I have an array with all the IDs from my friends and loop through this array and create an object of my custom class Friend (which receives all the necessary information) with each ID. I am adding this object to an array. This friend object makes a request to Facebook and processes the response to get the data I need.

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 is never called! only if I make a request in the main thread does it work:

 dispatch_async(dispatch_get_main_queue(), ^(void) { [facebook requestWithGraphPath:udid andDelegate:self]; }); 
+4
source share
1 answer

This is why in another thread you are not receiving a response:

Facebook will use NSURLConnection to fulfill requests. For the connection to work correctly, the start-up loop of the calling threads must work in the default start-up loop mode (read the NSURLConnection class link). When you use dispatch_async (), there is no default run cycle in run cycle mode (unless you are in the main send queue, so you are working in the main thread). Therefore, I suppose that the request has not even been sent (you can check that you sniff your network if you want).

So, in short, you should make your request to the main thread; since it is asynchronous, it will not freeze your application. Then, if processing the response is too expensive, process it in the background.

I really hope this helps.

My best.

+9
source

Source: https://habr.com/ru/post/1369470/


All Articles