[英]WCSession didReceiveUserInfo gets called but iPhone app stops background activity
I'm experiencing issues with my watchOS 2 app. 我的watchOS 2应用出现问题。 I'm using
[[WCSession defaultSession] transferUserInfo:request]
to transfer a dictionary of data to my iPhone app. 我正在使用
[[WCSession defaultSession] transferUserInfo:request]
将数据字典传输到我的iPhone应用程序。
In AppDelegate.m
in the iPhone app I have implemented -(void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo
在iPhone应用程序的
AppDelegate.m
中,我实现了-(void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo
When I send a transfer request from my watch, this method gets called (confirmed by a NSLog I created), but not all code is executed. 当我从手表发送传输请求时,此方法会被调用(由我创建的NSLog确认),但并非所有代码都会执行。 To be more specific, it calls the following method to post to Facebook, which also gets called but which will not be completely executed.
更具体地说,它调用以下方法发布到Facebook,该方法也会被调用,但不会完全执行。
- (void)postToFacebook:(NSString*)postMessage {
NSLog(@"begin");
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me/feed" parameters:@{@"message" : postMessage, @"privacy": @"{'value': 'SELF'}"} HTTPMethod:@"POST"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(@"done");
}];
}
}
}
NSLog "begin" is shown, but NSLog "done" isn't shown. 显示NSLog“开始”,但不显示NSLog“完成”。
I've tested running the method from my iPhone and everything is working fine. 我已经测试了在iPhone上运行该方法的一切,一切正常。 But when I try to call it from the watch using
transferUserInfo
, it isn't executed properly. 但是,当我尝试使用
transferUserInfo
从手表中调用它时,它执行不正确。
Now my question is, how I can assure that the background task within the iPhone app isn't killed before the method is completely done. 现在我的问题是,如何确保在该方法完全完成之前不会杀死iPhone应用程序中的后台任务。
A bit of a long shot, but try wrapping the code in a dispatch to the main queue: 有点远,但是尝试将代码包装在分派到主队列中:
- (void)postToFacebook:(NSString*)postMessage {
dispatch_sync(dispatch_get_main_queue(), ^{
if ([[FBSDKAccessToken currentAccess...etc...
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.