2013-06-26 71 views
0

我正在寻找一个清晰和完整的解释,以获得一组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。这可能会相当简化代码。这是一个很好的解决方案吗?如果是的话,我将如何建立这样的后台进程?

或者你会推荐什么其他解决方案?

回答

1

使用NSURLConns方法:+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler这样的:

// 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 sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *data, NSError *error) { 
      [[PFUser currentUser] setObject:data forKey:@"image"]; 
      [[PFUser currentUser] saveInBackground]; 
     }]; 
    } 
}];   

}

+0

但逻辑躲开我....你200楼的朋友,但总是做对于相同的PFUser的密钥..所以只有1图像使它解析 –

+0

没有必要在这里的线程..所有异步 –

+0

谢谢。我的帖子提到,“在我的应用程序的其他地方,我使用下面的代码来获取当前用户的图像:”这就是为什么我的文章中的代码只有一个PFUser。它看起来像使用队列和完成处理程序的方法应该是一个很好的解决方案。我会尽快给它一个。 – mkc842

0

我真的不知道该怎么做,但我可以通过FQL查询获得95%的回报。查看Facebook的FQL here的文档。循环确实不是一个好主意,尽管有一种方法可以将多个FBRequest组合成一个大的FBRequestConnection并一次启动 - 但是我建议不要这样做。

在我的一个应用程序中,我检查在Facebook事件中签入的用户朋友(交叉引用用户朋友再次列出所有与会者列表) - 我通过FQL查询来执行此操作。我明白这不是你想要的,但它可以帮助你理解这种方法。检查我自己的问题here的答案。

希望这有助于...

相关问题