2013-06-26 70 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的答案。

希望這有助於...

相關問題