@@ -46,9 +46,6 @@ @interface MCCastingApp ()
4646// Client defiend data source used to initialize the MCCommissionableDataProvider and, if needed, update the MCCommissionableDataProvider post initialization. This is necessary for the Commissioner-Generated passcode commissioning feature.
4747@property (nonatomic , strong ) id <MCDataSource> dataSource;
4848
49- // Track whether this is the first start (cold boot) or subsequent start (warm boot)
50- @property (atomic ) BOOL hasStartedBefore;
51-
5249@end
5350
5451@implementation MCCastingApp
@@ -132,61 +129,32 @@ - (NSError *)updateCommissionableDataProvider
132129
133130- (void )startWithCompletionBlock : (void (^)(NSError *))completion
134131{
135- ChipLogProgress (AppServer, " MCCastingApp.startWithCompletionBlock called (%s start)" , self.hasStartedBefore ? " warm" : " cold" );
136- self.hasStartedBefore = YES ;
137-
132+ ChipLogProgress (AppServer, " MCCastingApp.startWithCompletionBlock called" );
138133 VerifyOrReturn (_workQueue != nil && _clientQueue != nil , dispatch_async (self->_clientQueue , ^{
139- ChipLogError (AppServer, " MCCastingApp.startWithCompletionBlock failed: work queue or client queue is nil" );
140134 completion ([MCErrorUtils NSErrorFromChipError: CHIP_ERROR_INCORRECT_STATE]);
141135 }));
142136
143- // Start event loop task FIRST (synchronously) to ensure proper state
144- __block CHIP_ERROR err = chip::DeviceLayer::PlatformMgrImpl ().StartEventLoopTask ();
145- if (err != CHIP_NO_ERROR) {
146- ChipLogError (AppServer, " MCCastingApp.startWithCompletionBlock StartEventLoopTask failed: %s" , err.AsString ());
147- dispatch_async (self->_clientQueue , ^{
148- completion ([MCErrorUtils NSErrorFromChipError: err]);
149- });
150- // Early return to prevent double completion call
151- return ;
152- }
153-
154- // Only then start the casting app (on work queue)
155137 dispatch_async (_workQueue, ^{
156- CHIP_ERROR startErr = matter::casting::core::CastingApp::GetInstance ()->Start ();
157- if (startErr != CHIP_NO_ERROR) {
158- ChipLogError (AppServer, " MCCastingApp.startWithCompletionBlock CastingApp::Start failed: %s" , startErr.AsString ());
159- }
160-
138+ __block CHIP_ERROR err = matter::casting::core::CastingApp::GetInstance ()->Start ();
161139 dispatch_async (self->_clientQueue , ^{
162- completion ([MCErrorUtils NSErrorFromChipError: startErr ]);
140+ completion ([MCErrorUtils NSErrorFromChipError: err ]);
163141 });
164142 });
143+ __block CHIP_ERROR err = chip::DeviceLayer::PlatformMgrImpl ().StartEventLoopTask ();
144+ VerifyOrReturn (err == CHIP_NO_ERROR, dispatch_async (self->_clientQueue , ^{
145+ completion ([MCErrorUtils NSErrorFromChipError: err]);
146+ }));
165147}
166148
167149- (void )stopWithCompletionBlock : (void (^)(NSError *))completion
168150{
169151 ChipLogProgress (AppServer, " MCCastingApp.stopWithCompletionBlock called" );
170152 VerifyOrReturn (_workQueue != nil && _clientQueue != nil , dispatch_async (self->_clientQueue , ^{
171- ChipLogError (AppServer, " MCCastingApp.stopWithCompletionBlock failed: work queue or client queue is nil" );
172153 completion ([MCErrorUtils NSErrorFromChipError: CHIP_ERROR_INCORRECT_STATE]);
173154 }));
174155
175156 dispatch_async (_workQueue, ^{
176- // Stop the casting app first
177- CHIP_ERROR err = matter::casting::core::CastingApp::GetInstance ()->Stop ();
178- if (err != CHIP_NO_ERROR) {
179- ChipLogError (AppServer, " MCCastingApp.stopWithCompletionBlock CastingApp::Stop failed: %s" , err.AsString ());
180- }
181-
182- // Then stop the event loop task to transition WorkQueue to suspended state
183- CHIP_ERROR stopEventLoopErr = chip::DeviceLayer::PlatformMgrImpl ().StopEventLoopTask ();
184- if (stopEventLoopErr != CHIP_NO_ERROR) {
185- ChipLogError (AppServer, " MCCastingApp.stopWithCompletionBlock StopEventLoopTask failed: %s" , stopEventLoopErr.AsString ());
186- if (err == CHIP_NO_ERROR) {
187- err = stopEventLoopErr;
188- }
189- }
157+ __block CHIP_ERROR err = matter::casting::core::CastingApp::GetInstance ()->Stop ();
190158
191159 dispatch_async (self->_clientQueue , ^{
192160 completion ([MCErrorUtils NSErrorFromChipError: err]);
0 commit comments