FaceBook iOS SDK 批量请求和单一完成处理程序
FaceBook iOS SDK Batch Request & Single Completion Handler
我开始为 iOS 使用 Facebook SDK,我有点困惑,
我看到了这一小段代码,它展示了我们如何执行批处理请求,但是每个请求都有一个完成处理程序对我来说是荒谬的。当您发出单个批处理请求时,如何为每个请求创建一个完成处理程序?也许我不明白这个概念?肯定有对此的解释,但我现在没有看到它
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"user_likes"]) {
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]
initWithGraphPath:@"me" parameters:nil];
FBSDKGraphRequest *requestLikes = [[FBSDKGraphRequest alloc]
initWithGraphPath:@"me/likes" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMe
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
//TODO: process me information
}];
[connection addRequest:requestLikes
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
//TODO: process like information
}];
[connection start];
}
如果这是唯一的方法,我怎么知道我的所有请求何时都已执行?
为什么不只使用一个请求?在你的情况下,没有必要有两个。
使用类似
的东西
/me?fields=id,name,likes
我创建了一个 FBBatchHandler class 来自动组合结果和 return 调用者的单个完成处理程序。下面的来源和示例用法。它目前专为检索事件而设计(即最终结果是事件的 NSArray),但应该很容易将其重新用于其他任务。让我知道这是否适合你。
FBBatchHandler.h
#import <Foundation/Foundation.h>
#import "FBSDKGraphRequestConnection.h"
typedef void (^BatchFinishedHandler)(NSArray *result, NSError *error);
@interface FBBatchHandler : NSObject
- (instancetype)initWithConnection:(FBSDKGraphRequestConnection *)connection
finishedHandler:(BatchFinishedHandler)finishHandler;
- (void)addRequestResult:(id)result;
@end
FBBatchHandler.m
#import "FBBatchHandler.h"
@interface FBBatchHandler () <FBSDKGraphRequestConnectionDelegate>
@property (nonatomic, strong) FBSDKGraphRequestConnection *connection;
@property (nonatomic, strong) BatchFinishedHandler finishHandler;
@property (nonatomic, strong) NSMutableArray *batchResults;
@end
@implementation FBBatchHandler
#pragma mark - Instance
- (instancetype)initWithConnection:(FBSDKGraphRequestConnection *)connection
finishedHandler:(BatchFinishedHandler)finishHandler {
if (self = [super init]) {
_connection = connection;
_connection.delegate = self;
_finishHandler = finishHandler;
_batchResults = [NSMutableArray new];
}
return self;
}
#pragma mark - Public methods
- (void)addRequestResult:(id)result {
if ([result isKindOfClass:[NSDictionary class]]) {
[self.batchResults addObjectsFromArray:result[@"data"]];
}
}
#pragma mark - FBSDKGraphRequestConnectionDelegate
- (void)requestConnectionDidFinishLoading:(FBSDKGraphRequestConnection *)connection {
self.finishHandler([self.batchResults copy], nil);
}
- (void)requestConnection:(FBSDKGraphRequestConnection *)connection
didFailWithError:(NSError *)error {
self.finishHandler([self.batchResults copy], error);
}
@end
Example Usage
- (void)retrieveEvents {
NSDictionary *params = @{@"since": @"now", @"fields": @"id,name,description,start_time,end_time,location,picture"};
FBSDKGraphRequest *attending = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/events/attending" parameters:params];
FBSDKGraphRequest *created = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/events/created" parameters:params];
FBSDKGraphRequest *maybe = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/events/maybe" parameters:params];
FBSDKGraphRequest *notReplied = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/events/not_replied" parameters:params];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
FBBatchHandler *batchHandler = [[FBBatchHandler alloc] initWithConnection:connection
finishedHandler:^(NSArray *result, NSError *error) {
// Here's the "single" completion handler
}];
[connection addRequest:attending
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[batchHandler addRequestResult:result];
}];
[connection addRequest:created
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[batchHandler addRequestResult:result];
}];
[connection addRequest:maybe
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[batchHandler addRequestResult:result];
}];
[connection addRequest:notReplied
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[batchHandler addRequestResult:result];
}];
[connection start];
}
我开始为 iOS 使用 Facebook SDK,我有点困惑,
我看到了这一小段代码,它展示了我们如何执行批处理请求,但是每个请求都有一个完成处理程序对我来说是荒谬的。当您发出单个批处理请求时,如何为每个请求创建一个完成处理程序?也许我不明白这个概念?肯定有对此的解释,但我现在没有看到它
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"user_likes"]) {
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]
initWithGraphPath:@"me" parameters:nil];
FBSDKGraphRequest *requestLikes = [[FBSDKGraphRequest alloc]
initWithGraphPath:@"me/likes" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMe
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
//TODO: process me information
}];
[connection addRequest:requestLikes
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
//TODO: process like information
}];
[connection start];
}
如果这是唯一的方法,我怎么知道我的所有请求何时都已执行?
为什么不只使用一个请求?在你的情况下,没有必要有两个。
使用类似
的东西/me?fields=id,name,likes
我创建了一个 FBBatchHandler class 来自动组合结果和 return 调用者的单个完成处理程序。下面的来源和示例用法。它目前专为检索事件而设计(即最终结果是事件的 NSArray),但应该很容易将其重新用于其他任务。让我知道这是否适合你。
FBBatchHandler.h
#import <Foundation/Foundation.h>
#import "FBSDKGraphRequestConnection.h"
typedef void (^BatchFinishedHandler)(NSArray *result, NSError *error);
@interface FBBatchHandler : NSObject
- (instancetype)initWithConnection:(FBSDKGraphRequestConnection *)connection
finishedHandler:(BatchFinishedHandler)finishHandler;
- (void)addRequestResult:(id)result;
@end
FBBatchHandler.m
#import "FBBatchHandler.h"
@interface FBBatchHandler () <FBSDKGraphRequestConnectionDelegate>
@property (nonatomic, strong) FBSDKGraphRequestConnection *connection;
@property (nonatomic, strong) BatchFinishedHandler finishHandler;
@property (nonatomic, strong) NSMutableArray *batchResults;
@end
@implementation FBBatchHandler
#pragma mark - Instance
- (instancetype)initWithConnection:(FBSDKGraphRequestConnection *)connection
finishedHandler:(BatchFinishedHandler)finishHandler {
if (self = [super init]) {
_connection = connection;
_connection.delegate = self;
_finishHandler = finishHandler;
_batchResults = [NSMutableArray new];
}
return self;
}
#pragma mark - Public methods
- (void)addRequestResult:(id)result {
if ([result isKindOfClass:[NSDictionary class]]) {
[self.batchResults addObjectsFromArray:result[@"data"]];
}
}
#pragma mark - FBSDKGraphRequestConnectionDelegate
- (void)requestConnectionDidFinishLoading:(FBSDKGraphRequestConnection *)connection {
self.finishHandler([self.batchResults copy], nil);
}
- (void)requestConnection:(FBSDKGraphRequestConnection *)connection
didFailWithError:(NSError *)error {
self.finishHandler([self.batchResults copy], error);
}
@end
Example Usage
- (void)retrieveEvents {
NSDictionary *params = @{@"since": @"now", @"fields": @"id,name,description,start_time,end_time,location,picture"};
FBSDKGraphRequest *attending = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/events/attending" parameters:params];
FBSDKGraphRequest *created = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/events/created" parameters:params];
FBSDKGraphRequest *maybe = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/events/maybe" parameters:params];
FBSDKGraphRequest *notReplied = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/events/not_replied" parameters:params];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
FBBatchHandler *batchHandler = [[FBBatchHandler alloc] initWithConnection:connection
finishedHandler:^(NSArray *result, NSError *error) {
// Here's the "single" completion handler
}];
[connection addRequest:attending
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[batchHandler addRequestResult:result];
}];
[connection addRequest:created
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[batchHandler addRequestResult:result];
}];
[connection addRequest:maybe
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[batchHandler addRequestResult:result];
}];
[connection addRequest:notReplied
completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[batchHandler addRequestResult:result];
}];
[connection start];
}