【问题标题】:How do you know which FBRequest initiated the request didLoad: method?你怎么知道是哪个 FBRequest 发起了请求 didLoad: 方法?
【发布时间】:2012-06-08 14:30:19
【问题描述】:

从 facebook iOS API 文档中,我创建了两个方法来请求图表中的朋友列表或当前登录用户的详细信息。

- (IBAction)showMyFriends:(id)sender {
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[delegate facebook] requestWithGraphPath:@"me/friends" andDelegate:self];
    NSLog(@"Getting friends list");
}
- (IBAction)showMyDetails:(id)sender {
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[delegate facebook] requestWithGraphPath:@"me" andDelegate:self];
    NSLog(@"Getting my info");
}

到目前为止听起来很合理。响应这些调用的委托方法是:

- (void)request:(FBRequest *)request didLoad:(id)result
{
    NSLog(@"Got a request");

// Print out friends
//    NSArray * items = [NSArray alloc];
//    items = [(NSDictionary *)result objectForKey:@"data"];
//    for (int i=0; i<[items count]; i++) {
//        NSDictionary *friend = [items objectAtIndex:i];
//        long long fbid = [[friend objectForKey:@"id"]longLongValue];
//        NSString *name = [friend objectForKey:@"name"];
//        NSLog(@"id: %lld - Name: %@", fbid, name);
//    }


// Print out self username
    NSString *username = [result objectForKey:@"name"];
    NSLog(@"Username is %@", username);
    helloGreeting.text = [NSString stringWithFormat:@"Hello %@", username];

}

问题:在didLoad 中,如何检查当前调用与哪个图形请求相关?例如,在上面的代码中,我要么想打印出朋友列表,要么打印出用户名,所以我想我需要根据请求类型将代码包装在某些 case/switch 语句中。

我在 API 上找不到任何明显的东西,确保只执行相关响应代码的最佳方法是什么?

【问题讨论】:

    标签: ios facebook facebook-graph-api ios5


    【解决方案1】:

    如上所述,您可以检查请求对象以检查哪个请求生成了响应。您可以使用请求对象的 url 属性更方便地检查使用了哪个请求。

    例如获取好友列表的请求

    [facebook requestWithGraphPath:@"me/friends" andDelegate:self];
    

    您可以检查请求对象的 url 为:

    https://graph.facebook.com/me/friends
    

    以下请求 didLoad 的实现将检测好友列表请求并遍历每个好友。

    - (void)request:(FBRequest *)request didLoad:(id)result {
    
        // Friends request
        if([request.url rangeOfString:@"me/friends"].location != NSNotFound) {
    
            NSArray *friends = [result objectForKey:@"data"];
            for (int i=0;i<friends.count;i++) {
    
                NSDictionary *friend = [friends objectAtIndex:i];
                //NSLog([friend objectForKey:@"name"]);
    
            }  
        }
    }
    

    【讨论】:

      【解决方案2】:

      这就是您在“didLoad”中获取 FBRequest 变量的原因。您可以使用它来检查原始请求。这是一种废话解决方案,但至少您可以检查它是什么。

      【讨论】:

        【解决方案3】:

        实际上,Hackbook 似乎正在基于一个名为“currentAPICall”的 int 进行切换,为每个请求设置一个 int,然后在返回时检查它。所以这一切都是在主线程中完成的,我猜。

        我也有不同类型的不同结果对象。我最终查看结果并确定它是来自 /me 请求还是来自 /home。我有各种“如果”来查看返回的对象。无论如何都不理想。即

        if([result objectForKey:@"first_name"]){
            // back from getMe()
        }
        
        if ([result objectForKey:@"data"]) {
            // more checking here- I have two calls that return a data object
        }
        

        更新:这不适用于异步请求,其中大多数是,所以我使用上面的方法,检查 request.url,而不是,它就像一个魅力。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-17
          • 2010-12-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多