【问题标题】:Perform a function only after the first function is completed - ios仅在第一个功能完成后才执行功能 - ios
【发布时间】:2014-01-29 15:56:42
【问题描述】:

在我的应用中,我会在用户执行特定操作后推送本地通知。
当用户从推送的通知中打开应用程序时,我想将事件注册到跟踪系统(在我的例子中是 Mixpanel)。
我应该有一个密钥来注册从我的服务器获取的事件。

所以我想做的是在一个函数中获取密钥,在这个函数完成后,我想注册事件。

我试过performSelectorOnMainThread:withObject:waitUntilDone:,但没用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //some code here
    [self performSelectorOnMainThread:@selector(performHandShake)
                           withObject:nil
                        waitUntilDone:YES];
    [self performSelectorOnMainThread:@selector(registerLaunchOptionsDetails:) 
                           withObject:launchOptions
                        waitUntilDone:YES];
    //some code here
}

-(void)performHandShake
{
   //myParams here

    [[RKClient sharedClient] get:@"/handshake" usingBlock:^(RKRequest *request) {
        request.params = [RKParams paramsWithDictionary:myParams];
        request.method = RKRequestMethodGET;

        request.onDidLoadResponse = ^(RKResponse *response) {
            //Set the tracking key here 
        };

        request.onDidFailLoadWithError = ^(NSError *error) {
            NSLog(@"ERROR:%@",error);
        };
    }];
}

-(void)registerLaunchOptionsDetails:(NSDictionary *)launchOptions
{
    UILocalNotification *localNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNotification) {
       //Register the event here using the key 
    }
 }

问题是在performHandShake 完成之前执行的registerLaunchOptionsDetails 函数并且没有注册事件。

【问题讨论】:

  • 可以在握手函数的最后调用registerLaunchOptionsDetails函数吗?
  • 在这种情况下,您只需要使用 Grand Central Dispatch。在这些情况下建议使用队列
  • 尝试从 onDidLoadResponse 块调用 callregisterLaunchOptionsDetails?
  • @Ali 我试过了,但它对我不起作用,我认为原因是我没有完全理解它。有什么好的例子推荐的链接吗?
  • @alex 谢谢你,它有效。您可以添加您的评论作为答案,我会接受。

标签: ios objective-c performselector localnotification


【解决方案1】:

这对于您想要做的事情可能有点过头了,但是有一个类似于 async for node 的库。您可以将异步任务链接在一起和/或等到多个任务完成后再执行另一个操作。

ReactiveCocoa

【讨论】:

    【解决方案2】:

    当你执行-[RKClient get:usingBlock]时,它会创建一个网络操作,该操作将在后台执行并且方法会返回。

    这将清除registerLaunchOptionsDetails运行的路径,这将在网络操作完成之前发生。

    当网络操作成功完成并且您拥有所请求的数据时,request.onDidLoad 将运行。这就是为什么在那里调用registerLaunchOptionsDetails: 会起作用的原因。

    【讨论】:

    • 感谢[RKClient get:usingBlock]的解释。我不知道。这有助于我理解事物。
    【解决方案3】:

    request.onDidLoadResponse会在你需要的时候被调用,所以你需要从这个块调用registerLaunchOptionsDetails:。不要忘记通过performHandShakelaunchOptions 传递给这个块并创建对self 的弱引用以防止它保留在块中。 最终代码应该是这样的:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        //some code here
    
        [self performSelectorOnMainThread:@selector(performHandShake:) withObject:launchOptions waitUntilDone:YES];
    
    
        //some code here
    
    }
    
    -(void)performHandShake:(NSDictionary *)launchOptions
    {
        //myParams here
    
            [[RKClient sharedClient] get:@"/handshake" usingBlock:^(RKRequest *request){
    
            request.params = [RKParams paramsWithDictionary:myParams];
            request.method = RKRequestMethodGET;
    
            __weak AppDelegate *weakSelf = self;
            request.onDidLoadResponse = ^(RKResponse *response)
            {
    
                //Set the tracking key here
    
                // Here is registerLaunchOptionsDetails: call
                [weakSelf registerLaunchOptionsDetails:launchOptions];
            };
    
            request.onDidFailLoadWithError = ^(NSError *error)
            {
                NSLog(@"ERROR:%@",error);
            };
        }];
    
    }
    
    -(void)registerLaunchOptionsDetails:(NSDictionary *)launchOptions
    {
    
        UILocalNotification *localNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
    
        if (localNotification)
        {
            //Register the event here using the key 
    
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 2018-05-31
      • 2010-12-28
      • 1970-01-01
      相关资源
      最近更新 更多