Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c sendResponse上的WatchKit崩溃_Objective C_Xcode_Watchkit - Fatal编程技术网

Objective c sendResponse上的WatchKit崩溃

Objective c sendResponse上的WatchKit崩溃,objective-c,xcode,watchkit,Objective C,Xcode,Watchkit,我的应用程序正在崩溃,我无法跟踪错误 它告诉我坠机事件: 在-[UIWatchKitExtensionRequestAction sendResponse:],/SourceCache/BaseBoard/BaseBoard-98.3/BaseBoard/BSAction.m:221中断言失败 **Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'this request

我的应用程序正在崩溃,我无法跟踪错误

它告诉我坠机事件:

在-[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
GleaseController.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;
             }
         }];
    });
}

当一个完成块(如
didReceiveMemotentification:fetchCompletionHandler:
中的块)被意外调用两次时,这种崩溃经常发生。您没有发布任何代码示例,但首先要确保您没有在
handleWatchKitExtensionRequest:userInfo:reply:
中两次调用
reply()
,假设
userInfo[@“pfquery\U请求”]
存在,
if
语句将通过,代码将在if语句中运行,包括
回复(@{@“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)});
    }
}

我们不是魔术师,请在调用
-sendResponse
的地方提供代码和任何其他必要的代码。@Schemetral添加了一些代码,谢谢!!答对了,发现错误:我们能看到WatchKit扩展代码吗?您实际上在哪里调用这个和周围的代码?在我看来,
-application:handleWatchKitExtensionRequest:
被调用了两次。@schemetral刚刚添加了扩展代码。如果你看到任何东西或者有任何关于如何更好地设置它的建议,请告诉我,因为你似乎对所有这些都有很好的处理能力。谢谢这有助于我专注于正在发生的事情。我将我的
AppDelegate
代码发布到我正在执行的
handleWatchKitExtensionRequest:userInfo:reply:
。。。这是不是像你告诉我的那样,我需要两次呼叫
reply()
,以确保我没有这样做?还是我的好?谢谢正如@schemetral所提到的,您发布的代码正在调用
reply()
两次。啊!!你太棒了。我想我也要删除
userInfo[@“pfquery\u request”]
if语句。我将代码更新为只调用
reply()
一次,但它仍然会给我同样的错误/崩溃。你知道为什么吗?确保你已经重新运行了主机iOS应用程序,以确保你正在使用更新的代码。如果失败,请尝试重新安装。太棒了,让我试试!另外,我认为我现在甚至不需要使用
userInfo[@“pfquery\u request”],因为我使用的是一个本地静态json文件,所以在不存在
userInfo[@“pfquery\u request”]的情况下,您建议如何构造?谢谢您是否多次调用handleWatchKitExtensionRequest?我在我的
AppDelegate
中调用它,然后从我的WatchKit
InterfaceController
GleaseController
请求。我根据您的请求发布了
InterfaceController
GleaseController
代码,以显示该端。@SRMR
-将激活
,有时会多次调用,断点和检查。