如何应对异步事件(登录)? [英] How to react to asynchronous events (login)?

查看:181
本文介绍了如何应对异步事件(登录)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在它需要一个应用程序登录实施我的iPhone应用程序的一些Facebook的功能。登录本身不是一个问题了,它的工作。

I am implementing some Facebook functionality in my iPhone app right now which needs an app login. The login itself isn't a problem any more, it's working.

但由于是Facebook的SDK适用于事件的事实,我不知道如何执行我的行动来成功登录的反应。

But due to the fact that the Facebook SDK works with events, I don't know how to perform my action as a reaction to a successful login.

例如:

我有一个方法

- (void)refreshEvents;

此方法需要调用的登录方法,以确保能够实际执行的操作。于是我打电话 [自checkLogin] - 但现在该怎么办?我怎么能等待,这不是真正绑定到我的方法的事件吗?由于checkLogin触发的异步操作/事件,我不能只告诉它要等到进行登录。

This method needs to call the login method to make sure that the action can actually be performed. So I call [self checkLogin] - but what now? How can I wait for an event that's not really bound to my method? As checkLogin triggers asynchronous actions/events I cannot just tell it to wait until the login is performed.

在该方法

- (void)request:(FBRequest *)request didLoad:(id)result;

在处理所产生的访问令牌,我怎么能告诉它一直初始动作,所以它可以继续吗?

while handling the resulting access token, how can I tell it what's been the initial action, so it can continue?

我的想法是用一个实例变量为的控股的队列行动登录后执行一个数组,但这似乎是一个非常讨厌的解决办法。

My idea is to use an instance variable for an array that's holding a queue of "actions to perform after login", but this seems to be a really nasty workaround.

感谢您的帮助。

阿恩

推荐答案

的最简单的解决办法是通过一次的登录完成可以执行一个完成块。此外,你可以通过一个如果有一个错误,被调用块。在我的应用我都裹到您定义包含一个完成和错误块类中的响应的模式这一点。这是作为参数传递给异步方法通过。这里唯一的缺点是,你必须写在适当的时候,以执行块登录。

The simplest solution would be to pass a completion block that can be executed once the login completes. In addition, you could pass a block that gets called if there is an error. In my applications I have wrapped this into a 'responder' pattern where you define a class that contains a completion and error block. That is passed as an argument to asynchronous methods. The only catch here is that you have to write the login to 'execute' the blocks at the appropriate time.

我已经在使用异步API的(包括Facebook的SDK)应用程序中实现这种模式几次。

I have implemented this pattern several times on applications that use asynchronous API's (including the Facebook SDK).

UPDATE(添加了一些code样本):

UPDATE (added some code samples):

在最简单的形式,你可以只添加一个完成的块。首先,你会想为您完成块的typedef。在这种情况下,我们将使用不返回值,并有一个ID输入块:

In its simplest form, you could just add a completion block. First, you would want to create a typedef for your completion block. In this case, we'll use a block that returns no value and has an id input:

typedef void (^successBlock)(id);

接下来,您可以创建与Facebook类的互动创建了一个门面的控制器。在这个门面,你可以创建一个具有这样的签名(这里假设你有型successBlock的伊娃)的登录方式:

Next, you could create a controller that creates a facade for your interaction with the Facebook class. In this facade, you could create a login method that has a signature like this (this assumes you have an iVar of type successBlock):

- (void)loginWithCompletionHandler:(successBlock)handler
{
    // If you aren't using ARC - you'll need to use Block_copy(handler)
    completionHandler = handler;

    [facebook authorize:facebookSettings.defaultPermissions 
               delegate:self];
}

最后,在FBSessionDelegate实现,可以执行此块:

Finally, in your FBSessionDelegate implementation, you can execute this block:

#pragma mark - FBSessionDelegate Implementation

- (void)fbDidLogin
{
    // Store Facebook token, etc.....

    if( completionHandler )
    {
        // You could pass any data you want in this data property
        completionHandler(data);
    }
}

}

我上面提到的响应模式比这更复杂一些,但这应该足以让你去。

The 'Responder' pattern I mentioned above is a bit more involved than this, but this should be enough to get you going.

这篇关于如何应对异步事件(登录)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆