简体   繁体   中英

Load data asynchronously after notification on apple watch

I'm receiving a notification that has, among other things, an ID in its payload. This ID is simply there so I can fetch the actual object on the server.

Right now, I only communicate using the AppDelegate HandleWatchKitExtensionRequest , and the .OpenParentApplication from the Watchkit.

Using that, if I don't make any webservice calls, it's all fine. Once I try to call the server, I get an error saying that the parent App never calls reply() .

So obviously I'm thinking that the async calls makes it fail.

What's the best way to achieve this? I have a feeling I'm doing this wrong but I have no other real solution from what I've found online.

Right now it looks like this

  1. [Watch] Remote Notification received
  2. [Watch] Open parent application with ID in dictionary
  3. [Parent] Call webservice ( Fails here )
  4. [Parent] Send results to the watch (some strings and an image )

Note : I'm using Xamarin, but any Obj-C/Swift code will suffice, and I need to be compatible with WatchOS 1, therefore I can't really use WCSession/WatchConnectivity framework, can I?

been in very same situation, and hope following will help you. So, when watchkit app calls the main app, begin background task, load the things you are loading, and end the bg task. Like this:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
__block UIBackgroundTaskIdentifier watchKitHandler;
    watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
                                                                   expirationHandler:^{
                                                                       watchKitHandler = UIBackgroundTaskInvalid;
                                                                   }];
//load the things here.
reply(@{@"hehe":@"hehe123"});
dispatch_after( dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 10. ), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
        [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
    } );
}

Hope this was useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM