WatchKit 在发送响应时崩溃

WatchKit crash on sendResponse

我的应用程序崩溃了,我无法找到错误。

它在崩溃时告诉我:

Assertion failure in -[UIWatchKitExtensionRequestAction sendResponse:], /SourceCache/BaseBoard/BaseBoard-98.3/BaseBoard/BSAction.m:221

 **Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'this request has been neutered - you can't call -sendResponse: twice nor after encoding it'**

 **First throw call stack:
(0x1823342d8 0x193b580e4 0x182334198 0x1831e8ed4 0x188812ab4 0x10003cf94 0x10004708c 0x188812a08 0x18745dab4 0x18871f778 0x10003cfd4 0x10003cf94 0x10004ab54 0x10004c248 0x19438922c 0x194388ef0)
libc++abi.dylib: terminating with uncaught exception of type NSException**

我试过设置一个异常断点,但仍然无法找到它。有什么想法吗?

谢谢!

编辑:

AppDelegate.m:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
    if (userInfo[@"pfquery_request"]) {
        NSLog(@"Starting PFQuery"); // won't print out to console since you're running the watch extension

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        NSString *dateToday = [formatter stringFromDate:[NSDate date]];
        NSString *dateTodayShort = [dateToday substringToIndex:[dateToday length] -6];

        // Get JSON file path
        NSString *JSONFilePath = [[NSBundle mainBundle] pathForResource:@"Days" ofType:@"json"];
        NSData *JSONData = [NSData dataWithContentsOfFile:JSONFilePath];
        NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];
        NSArray *days;
        days = JSONDictionary[@"days"];

        // Iterate thru JSON to find Data for Today
        NSObject *todayJson;
        for (NSObject *object in days) {
            NSString *dateObject = [object valueForKey:@"day"];
            if ([dateObject isEqualToString:dateTodayShort]) {
                todayJson = object;
                NSString *textToday = [todayJson valueForKey:@"text"];

                // App Group
                NSString *container = @"group.com.rr.mm";
                NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
                [defaults setValue:textToday forKey:@"textSaved"];
            }
        }
        reply(@{@"success": @(YES)});
    }
    reply(@{@"success": @(NO)});
}

我在我的 WatchKit InterfaceController.m 和我的 WatchKit GlanceController.m 中使用相同的东西请求数据:

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [WKInterfaceController
         openParentApplication:@{@"pfquery_request": @"dumm_val"}
         reply:^(NSDictionary *replyInfo, NSError *error) {
             NSLog(@"User Info: %@", replyInfo);
             NSLog(@"Error: %@", error);

             if ([replyInfo[@"success"] boolValue]) {

                 // Get Saved data
                 NSString *container = @"group.com.rr.mm";
                 NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
                 NSString *text = [defaults valueForKey:@"textSaved"];

                 // Set Saved Data to Global String
                 textGlobal = text;

                 // Set text Label
                 self.textVerseLabelWatch.text = text;
             }
         }];
    });
}

这种崩溃通常发生在完成块(例如 didReceiveRemoteNotification:fetchCompletionHandler: 中的块)被意外调用两次时。您还没有发布任何代码示例,但首先要确保您没有在 handleWatchKitExtensionRequest:userInfo:reply:.

中两次调用 reply() 之类的东西

假设userInfo[@"pfquery_request"]存在,if语句会通过,if语句里面的代码会运行,包括reply(@{@"success": @(YES)});。紧接着,不管if语句是否通过,它都会运行reply(@{@"success": @(NO)});。这就是它被调用两次的原因。

改为使用以下内容。

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
    if (userInfo[@"pfquery_request"]) {
        NSLog(@"Starting PFQuery"); // won't print out to console since you're running the watch extension

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        NSString *dateToday = [formatter stringFromDate:[NSDate date]];
        NSString *dateTodayShort = [dateToday substringToIndex:[dateToday length] -6];

        // Get JSON file path
        NSString *JSONFilePath = [[NSBundle mainBundle] pathForResource:@"Days" ofType:@"json"];
        NSData *JSONData = [NSData dataWithContentsOfFile:JSONFilePath];
        NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];
        NSArray *days;
        days = JSONDictionary[@"days"];

        // Iterate thru JSON to find Data for Today
        NSObject *todayJson;
        for (NSObject *object in days) {
            NSString *dateObject = [object valueForKey:@"day"];
            if ([dateObject isEqualToString:dateTodayShort]) {
                todayJson = object;
                NSString *textToday = [todayJson valueForKey:@"text"];

                // App Group
                NSString *container = @"group.com.rr.mm";
                NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
                [defaults setValue:textToday forKey:@"textSaved"];
            }
        }
        reply(@{@"success": @(YES)});
    } else {
        reply(@{@"success": @(NO)});
    }
}