WatchKit在handleWatchKitExtensionRequest中的块内返回reply(): [英] WatchKit return reply() inside a block in handleWatchKitExtensionRequest:

查看:100
本文介绍了WatchKit在handleWatchKitExtensionRequest中的块内返回reply():的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这样的 SO帖子,其中显然是数据被提取并返回到Watch扩展,如下所示:

I saw this SO post where apparently data was being fetched and returned to the Watch extension like so:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
  if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
 {
  // get data
  // ...
  reply( data );
 }
}

但是当我尝试像这样获取网络数据后在一个块内调用"reply()"时:

But when I try to call 'reply()' inside a block after getting network data like so:

__block UIBackgroundTaskIdentifier watchKitHandler;

watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
                                                               expirationHandler:^{
                                                                   watchKitHandler = UIBackgroundTaskInvalid;
                                                               }];   
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{ 

NSMutableDictionary *response = [NSMutableDictionary dictionary];
[ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){

        if (succeeded)
        {
            [response setObject:@"update succeded" forKey:@"updateKey"];
            reply(response);

        }
        else
        {
            if (error)
            {
                [response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"]; 
                reply(response);
            }
            else
            {
                [response setObject:@"update failed with no error" forKey:@"updateKey"];
                reply(response);
            }
        }
    }];
}

dispatch_after(dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 180), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
});

我收到此错误:

"iPhone应用程序中的UIApplicationDelegate从未在-[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]中调用reply()"

"The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]"

所以我想我必须立即调用reply(),并且在WatchKit唤醒父应用程序后发送新的网络数据的唯一方法是使用MMWormhole吗?

So I guess I have to call reply() immediately and the only way to send fresh network data after WatchKit wakes up the parent app is to use MMWormhole?

推荐答案

要确保异步数据获取不会立即返回(不调用回复),请尝试以下操作:

To make sure that your asynchronous data fetch does not return immediately (without calling reply), you may try the following:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{ 
    __block UIBackgroundTaskIdentifier watchKitHandler;

    watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
                                                               expirationHandler:^{
                                                                   watchKitHandler = UIBackgroundTaskInvalid;
                                                               }];  

   NSMutableDictionary *response = [NSMutableDictionary dictionary];

   dispatch_semaphore_t sema = dispatch_semaphore_create(0);

   [ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){

        if (succeeded)
        {
            [response setObject:@"update succeded" forKey:@"updateKey"];
            reply(response);

        }
        else
        {
            if (error)
            {
                [response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"]; 

                dispatch_semaphore_signal(sema);

                reply(response);
            }
            else
            {
                [response setObject:@"update failed with no error" forKey:@"updateKey"];

                dispatch_semaphore_signal(sema);

                reply(response);
            }
        }
    }];

    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    dispatch_after(dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
  });
}

这篇关于WatchKit在handleWatchKitExtensionRequest中的块内返回reply():的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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