-->

从与FacebookSDK异步块方法返回值(Method returning value from

2019-07-20 17:26发布

我所试图做的是一个网站包装为Facebook的iOS SDK。 基本想法是,我的ViewController应该做什么,但呈现前。 我的朋友们,将用一个简单的调用来获取像

self.friends = [FacebookWrapper myFriends];
[self.tableView reloadData];

我的包装myFriends方法应该是这样的

+ (NSArray *)myFriends
{
   __block NSArray *friends = nil;
   [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
    if(FB_ISSESSIONOPENWITHSTATE(status)) {
    [FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id data, NSError *error) {
       CFRunLoopStop(CFRunLoopGetCurrent());
        if(error) {
            return;
        }
        NSArray *friendsData = (NSArray *)[data data];
        NSMutableArray *fbFriends = [NSMutableArray array];
        for(id friendData in friendsData) {
            Friend *friend = [Friend friendWithDictionary:friendData];
            fbFriends addObject:friend];
        }
        friends = [NSArray arrayWithArray:fbFriends];
    }];
    CFRunLoopRun();
    }
  }];
  return friends;
}

问题是,openActiveSessionWithReadPermissions和startForMyFriendsWithCompletionHandler是异步块,这样的方法会返回块完成他们的任务之前。

任何帮助将非常感激。

Answer 1:

我在过去创建了类似的包装,并调用我的包装方法时,我的方法是传递一个“完成块”; 那么这个完成块被触发一次所有的异步调用完成后运行,它接收所有数据的方法将在同步情况下返回(在你的情况下,朋友阵列)。

为了说明 - 你可以有你的“myFriends”的方法重新定义为:

+ (void)myFriendsWithCompletionBlock:(void (^)(NSArray *friends))completionBlock;

然后在实施中,之后friends = [NSArray arrayWithArray:fbFriends]; 行,你想补充一点:

if (completionBlock != nil) {
    completionBlock(friends);
}

...并删除return在最后陈述。

最后,您的视图控制器(或使用该方法的对象上,你会做这样的事情:

[FacebookWrapper myFriendsWithCompletionBlock:^(NSArray *friends){
    // do what you need to do with the friends array
}];

当然,这仍然是异步的 - 但有周围没有办法,因为这是Facebook的SDK如何编译(和,说句公道话,这可能是做到这一点的最好办法 - 等待请求完成同步将是可怕的!)

编辑:我注意到你也的情况下,失败的包装方法返回; 在这种情况下,而不是返回你会做这样的事情的:

if (completionBlock != nil) {
    completionBlock(nil);
}

这将导致friends阵列是nil ,当你完成块被称为-那么你可以把这个错误但有似乎是适当的给你。

希望这有助于!



Answer 2:

如果你分派asynchronouos块,你可以与你沟通UIViewController通过回调它的子类:

[self someSelectorWithCallbackData:stuffWhichYouWantToGiveBack];

这将调用self获得由块捕获,因此将正常工作。 从相关的方法,你可以刷新视图/重装的tableview /所需跳舞的夹具。

根据上下文,您可能需要__block范围self ,如

__block UIViewController *bsself = self;

但是,如果你是后者,要小心避免保留环(构建和分析工具是在指出这一点相当不错)。



Answer 3:

认为你需要使用protol @class Webservice的;

@protocol WebserviceDelegate
@optional

-(void)webservice:(Webservice *)webservice didFetchPosts:(NSArray *)posts;
-(void)webservice:(Webservice *)webservice didFetchComments:(NSArray *)comments forPostID:(NSString *)postID launchComments:(BOOL)launch;
-(void)webservice:(Webservice *)webservice didLoginWithUser:(User *)user;
-(void)webservice:(Webservice *)webservice didVoteWithSuccess:(BOOL)success forObject:(id)object direction:(BOOL)up;

@end

@interface Webservice : NSObject {
    __weak id <WebserviceDelegate> delegate;
}
//Delegate
@property (weak) id <WebserviceDelegate> delegate;



-(void)getHomepage {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSURLResponse *response;
        NSError *error;

        // Create the URL Request
        NSMutableURLRequest *request = [Webservice NewGetRequestForURL:[NSURL URLWithString:@"https://www.hnsearch.com/bigrss"]];

        // Start the request
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


        //Handle response
        //Callback to main thread
        if (responseData) {
            NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSStringEncodingConversionAllowLossy];

            if (responseString.length > 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self parseIDsAndGrabPosts:responseString];
                });
            }
            else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [delegate webservice:self didFetchPosts:nil];
                });

            }
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [delegate webservice:self didFetchPosts:nil];
            });
        }


    });
}


-(void)parseIDsAndGrabPosts:(NSString *)parseString {
    // Parse String and grab IDs
    NSMutableArray *items = [@[] mutableCopy];
    NSArray *itemIDs = [parseString componentsSeparatedByString:@"<hnsearch_id>"];
    for (int xx = 1; xx < itemIDs.count; xx++) {
        NSString *idSubString = itemIDs[xx];
        [items addObject:[idSubString substringWithRange:NSMakeRange(0, 13)]];
    }


    // Send IDs back to HNSearch for Posts
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSURLResponse *response;
        NSError *error;

        // Create Request String
        NSString *requestString = @"http://api.thriftdb.com/api.hnsearch.com/items/_bulk/get_multi?ids=";
        for (NSString *item in items) {
            requestString = [requestString stringByAppendingString:[NSString stringWithFormat:@"%@,", item]];
        }

        // Create the URL Request
        NSMutableURLRequest *request = [Webservice NewGetRequestForURL:[NSURL URLWithString:requestString]];

        // Start the request
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


        //Handle response
        //Callback to main thread
        if (responseData) {
            NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];

            if (responseArray) {
                NSMutableArray *postArray = [@[] mutableCopy];
                for (NSDictionary *dict in responseArray) {
                    [postArray addObject:[Post postFromDictionary:dict]];
                }

                NSArray *orderedPostArray = [self orderPosts:postArray byItemIDs:items];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [delegate webservice:self didFetchPosts:orderedPostArray];

                    // Update Karma for User
                    if ([HNSingleton sharedHNSingleton].User) {
                        [self reloadUserFromURLString:[NSString stringWithFormat:@"https://news.ycombinator.com/user?id=%@", [HNSingleton sharedHNSingleton].User.Username]];
                    }
                });
            }
            else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [delegate webservice:self didFetchPosts:nil];
                });

            }
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [delegate webservice:self didFetchPosts:nil];
            });
        }


    });
}


文章来源: Method returning value from asynchronous block with FacebookSDK