我正在寻找一个清晰和完整的解释,以获得一组Facebook朋友(他们的Facebook ID存储在一个数组中)的个人资料图片的最佳方式。在别处在我的应用程序,我用下面的代码来获取当前用户的图像:获取Facebook背景中的一组朋友的Facebook个人资料图片
...
...
// Create request for user's Facebook data
FBRequest *request = [FBRequest requestForMe];
// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"no fb error");
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
// Download the user's facebook profile picture
self.imageData = [[NSMutableData alloc] init]; // the data will be loaded in here
// URL should point to https://graph.facebook.com/{facebookId}/picture?type=large&return_ssl_resources=1
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=square&return_ssl_resources=1", userData[@"id"]]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:pictureURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:2.0f];
// Run network request asynchronously
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (!urlConnection) {
NSLog(@"Failed to download picture");
}
}
}];
}
// Called every time a chunk of the data is received
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.imageData appendData:data]; // Build the image
}
// Called when the entire image is finished downloading
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[PFUser currentUser] setObject:self.imageData forKey:@"image"];
[[PFUser currentUser] saveInBackground];
}
这种做法似乎已经在后台下载数据的优势,但现在看来似乎可能会非常棘手实现它作为for
循环的一部分,我循环了一组用户的Facebook好友来获取他们的个人资料图片,因为它调用了多个委托方法,然后我必须跟踪委托方法中哪些朋友的图像数据被接收。
假设通过这些朋友迭代的for循环将在主线程上执行并且在数据收集完成之前完成之前,是否有可能跟踪哪些朋友的图像数据正在委托方法中被接收迭代(我相信)?
我想解决这个问题的一种方法是在后台线程中执行整个for循环,我不会迭代到下一个朋友,直到当前人收集其所有数据。然后我不需要使用异步调用NSURLConnection
。这可能会相当简化代码。这是一个很好的解决方案吗?如果是的话,我将如何建立这样的后台进程?
或者你会推荐什么其他解决方案?
但逻辑躲开我....你200楼的朋友,但总是做对于相同的PFUser的密钥..所以只有1图像使它解析 –
没有必要在这里的线程..所有异步 –
谢谢。我的帖子提到,“在我的应用程序的其他地方,我使用下面的代码来获取当前用户的图像:”这就是为什么我的文章中的代码只有一个PFUser。它看起来像使用队列和完成处理程序的方法应该是一个很好的解决方案。我会尽快给它一个。 – mkc842