gpt4 book ai didi

ios - WatchKit在handleWatchKitExtensionRequest中的 block 内返回Reply():

转载 作者:行者123 更新时间:2023-12-01 16:33:46 25 4
gpt4 key购买 nike

我看到this这样的帖子,显然是在那里提取数据并像下面这样返回到Watch扩展:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
{
// get data
// ...
reply( data );
}
}
但是,当我尝试像这样获取网络数据后在一个块内调用“reply()”时:
__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()。”

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

最佳答案

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

- (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];
});
}

关于ios - WatchKit在handleWatchKitExtensionRequest中的 block 内返回Reply():,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30137019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com