2012-04-18 58 views
2

我做了下面的代碼讓我的朋友的信息異性。
首先,我發送了獲取所有好友ID的請求。我再次發送請求以獲取朋友的信息(姓名,照片等)。
但我有350個朋友,我發送了350個請求。花1分鐘時間確實很慢。
我可以使過程更快嗎?我可以更快地提出請求嗎? (臉譜圖api)

- (void)request:(FBRequest *)request didLoad:(id)result 
{ 
if (request == requestFriends) { 
    NSMutableArray *tempKeys = [NSMutableArray array]; 

    for (NSDictionary *dic in [result objectForKey:@"data"]) { 
     [tempKeys addObject:[dic objectForKey:@"id"]]; 
    } 

    NSMutableDictionary *params = [NSMutableDictionary dictionary]; 

    if ([self.myGender isEqualToString:@"male"]) { 
     params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"id,name,gender,age,location", @"field", nil]; 
    } else if ([self.myGender isEqualToString:@"female"]) { 
     params =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"id,name,age,gender,work,school", @"field", nil]; 
    } 

    for (NSString *key in tempKeys) { 
     [requestArray addObject: [delegate.facebook requestWithGraphPath:key andParams:params andDelegate:self]]; 
    } 

    i = tempKeys.count; 
} else if (request == self.myPicRequest) { //고화질 프로필 사진 받아오는 부분 
    NSArray *arr = [result objectForKey:@"data"]; 
    for (NSDictionary *result in arr) { 
     if([[result objectForKey:@"type"]isEqualToString:@"profile"]) { 
      profileRequest = [delegate.facebook requestWithGraphPath:[result objectForKey:@"cover_photo"] andDelegate:self]; //프로필의 아이디로 다시 리퀘스트 
     } 
    } 
} else if (request == self.profileRequest) { 
    NSURL *url = [NSURL URLWithString:[[[result objectForKey:@"images"] objectAtIndex:3] objectForKey:@"source"]]; 
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 
    CGRect rect = CGRectMake(0, 60, 360, 360); //중간부분을 크롭 
    [self.candidatePicArray addObject:[self imageByCropping:image toRect:rect]]; 
    NSLog(@"이미지들어간다"); 
} else {   
    for (FBRequest *req in requestArray) { 
     if (request == req) { 
      if (![[result objectForKey:@"gender"]isEqual:myGender]) { 
       [candidateIdArray addObject:[result objectForKey:@"id"]]; 
       [candidateNameArray addObject:[result objectForKey:@"name"]]; 

       myPicRequest = [delegate.facebook requestWithGraphPath:[NSString stringWithFormat:@"%@/albums", [result objectForKey:@"id"]] andDelegate:self]; 

       if ([result objectForKey:@"birth"]) { 
       [candidateAgeArray addObject:[result objectForKey:@"birth"]]; 
       } 

       if ([result objectForKey:@"Location"]) { 
       [candidateLocationArray addObject:[[result objectForKey:@"Location"] objectForKey:@"name"]]; 
       } 

       if ([[result objectForKey:@"work"] objectAtIndex:0]) { 
       [candidateWorkArray addObject:[[[[result objectForKey:@"work"] objectAtIndex:0] objectForKey:@"employer"] objectForKey:@"name"]]; 
       } 
       NSLog(@"girl!"); 
      } 
      j++; 
//    NSLog(@"candidateNameArray : %@", [result objectForKey:@"name"]); 
     } 
    } 
} 

NSLog(@"i = %d, j = %d", i , j); 
[progressView setProgress:(float)(j/i) animated:YES]; 

if(i == j) { 
    [self performSegueWithIdentifier:@"SEGUE_START" sender:nil]; 
} 

}

回答

2

有對等在此的其他問題配料請求一些線索:

Batch calls with Facebook Graph API & PHP

雖然它使用的PHP,你可能會得到一些線索。

+0

Thanks!我不知道PHP。但批處理的概念可以幫助我。 – 2012-04-18 05:55:03

2

您不必爲每個朋友發送請求,您可以一次完成所有操作。

嘗試提出請求,如:

https://graph.facebook.com?ids=iduser1,iduser2,iduser3,iduser4&fields=email,gender,age,name 

這種方式是快了很多。 Regards

+0

優秀的答案隊友! – 2012-04-20 21:34:40

+0

iOS SDK有可能嗎? – 2012-04-28 13:27:49

+0

發送請求給朋友可能在ios5中,或者只能使用在iOS6中引入的facebook sdk來完成? – 2013-01-03 11:59:17