简体   繁体   English

你怎么知道哪个FBRequest发起了请求didLoad:方法?

[英]How do you know which FBRequest initiated the request didLoad: method?

From the facebook iOS API documentation, I've create two methods that request either the friends list from the graph, or details about the currently logged in user. 从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");
}

Sounds reasonable so far. 到目前为止听起来很合理。 The delegate method that responds from these calls is : 响应这些调用的委托方法是:

- (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];

}

Question : In the didLoad , how can you check which graph request the current invocation relates to? 问题 :在didLoad ,如何检查当前调用与哪个图形请求相关? For example, in the above code I would either want to print out the friends list, or print out the username, so I would image I need to wrap the code in some case/switch statement dependant on the request type. 例如,在上面的代码中,我要么打印好友列表,要么打印出用户名,所以我想图像我需要根据请求类型将代码包装在某些case / switch语句中。

I couldn't find anything obvious on the API, what is the best approach for ensuring only relevant response code is executed? 我在API上找不到任何明显的东西,确保只执行相关响应代码的最佳方法是什么?

As noted above, you can inspect the request object to check which request generated the response. 如上所述,您可以检查请求对象以检查生成响应的请求。 You can use the url property of the request object to more conveniently check which request was used. 您可以使用请求对象的url属性来更方便地检查使用了哪个请求。

Eg for the request to get a friends list 例如,获取朋友列表的请求

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

You can check for the url of the request object being: 您可以检查请求对象的URL是:

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

The following implementation of request didLoad will detect a friends list request and iterate over each friend. 请求的以下实现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"]);

        }  
    }
}

That is why you get the FBRequest variable back in "didLoad". 这就是你在“didLoad”中获得FBRequest变量的原因。 You can use that to inspect the original request. 您可以使用它来检查原始请求。 It's kind of a crap solution, but at least you can inspect what it was. 这是一种垃圾解决方案,但至少你可以检查它是什么。

Actually looks like Hackbook is doing a switch based on an int called "currentAPICall", setting an int for each request, then checking on that in the return. 实际上看起来像Hackbook正在做一个基于名为“currentAPICall”的int的开关,为每个请求设置一个int,然后在返回中检查它。 So it's all done in the main thread, I'm guessing. 所以这一切都是在主线程中完成的,我猜。

I also have different result objects, of different types. 我也有不同类型的结果对象。 I end up looking at the result and determining if it's from the /me request or from the /home. 我最终查看结果并确定它是来自/我的请求还是来自/ home。 I have various "ifs" looking at the returning object. 我有各种各样的“ifs”看着返回的对象。 Not ideal by any means. 无论如何都不理想。 Ie

if([result objectForKey:@"first_name"]){
    // back from getMe()
}

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

Update: this doesn't work for asynchronous requests, and most of these are, so I used the method above, checking request.url, instead, and that works like a charm. 更新:这不适用于异步请求,其中大多数是,所以我使用上面的方法,检查request.url,而不是像魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM