WatchKit在sendResponse上崩溃

时间:2015-05-24 01:32:20

标签: objective-c xcode watchkit

我的应用程序崩溃,我无法追踪错误。

它告诉我坠机事件:

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

2 个答案:

答案 0 :(得分:2)

假设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)});
    }
}

答案 1 :(得分:1)

当完成块(例如didReceiveRemoteNotification:fetchCompletionHandler:中的完成块)被意外调用两次时,通常会发生此崩溃。您尚未发布任何代码示例,但首先要确保您未在​​reply()内完成两次调用handleWatchKitExtensionRequest:userInfo:reply: