diff --git a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m index 3daec55b4913c0..e83a9599dc29be 100644 --- a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m +++ b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m @@ -708,6 +708,7 @@ - (void)test011_ReadCachedAttribute XCTestExpectation * subscribeExpectation = [self expectationWithDescription:@"Subscription complete"]; NSLog(@"Subscribing..."); + __block void (^reportHandler)(NSArray * _Nullable value, NSError * _Nullable error); [device subscribeWithQueue:queue minInterval:2 maxInterval:60 @@ -715,10 +716,20 @@ - (void)test011_ReadCachedAttribute cacheContainer:attributeCacheContainer attributeReportHandler:^(NSArray * value) { NSLog(@"Received report: %@", value); + if (reportHandler) { + __auto_type handler = reportHandler; + reportHandler = nil; + handler(value, nil); + } } eventReportHandler:nil errorHandler:^(NSError * error) { NSLog(@"Received report error: %@", error); + if (reportHandler) { + __auto_type handler = reportHandler; + reportHandler = nil; + handler(nil, error); + } } subscriptionEstablished:^() { [subscribeExpectation fulfill]; @@ -774,7 +785,6 @@ - (void)test011_ReadCachedAttribute [self waitForExpectations:[NSArray arrayWithObject:newSubscriptionEstablished] timeout:kTimeoutInSeconds]; __auto_type reportExpectation = [self expectationWithDescription:@"Report handler called"]; - __block void (^reportHandler)(NSArray * _Nullable value, NSError * _Nullable error); reportHandler = ^(NSArray * _Nullable value, NSError * _Nullable error) { NSLog(@"Report received: %@, error: %@", value, error); for (MTRAttributeReport * report in value) {