@@ -70,12 +70,19 @@ - (BFTask *)performAsyncInitializationForEntity:(MBLEntityEvent *)entity
7070 return [BFTask taskWithResult: nil ];
7171 }
7272
73- return [[BFTask taskFromMetaWearWithBlock: ^id _Nonnull{
74- assert (entity.index == 0xFF );
73+ return [[BFTask taskFromMetaWearWithBlock: ^id _Nonnull {
74+ NSError *error = nil ;
75+ if (entity.index != 0xFF ) {
76+ error = [NSError errorWithDomain: kMBLErrorDomain
77+ code: kMBLErrorOperationInvalid
78+ userInfo: @{NSLocalizedDescriptionKey : @" Can't initialize entity that's already initialized" }];
79+ }
7580 if (self.entities .count >= self.maxEntities ) {
76- NSError * error = [NSError errorWithDomain: kMBLErrorDomain
81+ error = [NSError errorWithDomain: kMBLErrorDomain
7782 code: kMBLErrorInsufficientMemory
7883 userInfo: @{NSLocalizedDescriptionKey : [NSString stringWithFormat: @" MetaWear out of memory, can't perform action. Reset the MetaWear and use no more than %d entities" , self .maxEntities]}];
84+ }
85+ if (error) {
7986 return [BFTask taskWithError: error];
8087 }
8188 [self .entities addObject: entity];
@@ -98,7 +105,12 @@ - (BFTask *)performAsyncDeinitializationForEntity:(MBLEntityEvent *)entity
98105 }
99106
100107 return [[BFTask taskFromMetaWearWithBlock: ^id _Nonnull{
101- assert (entity.index != 0xFF );
108+ if (entity.index == 0xFF ) {
109+ NSError *error = [NSError errorWithDomain: kMBLErrorDomain
110+ code: kMBLErrorOperationInvalid
111+ userInfo: @{NSLocalizedDescriptionKey : @" Can't deinitialize entity that's not initialized" }];
112+ return [BFTask taskWithError: error];
113+ }
102114 return [self .removeEntity writeByteAsync: entity.index];
103115 }] continueOnMetaWearWithSuccessBlock: ^id _Nullable (BFTask * _Nonnull task) {
104116 entity.index = 0xFF ;
@@ -114,7 +126,12 @@ - (BFTask *)performAsyncActivationForEntity:(MBLEntityEvent *)entity
114126 }
115127
116128 return [BFTask taskFromMetaWearWithBlock: ^id _Nonnull{
117- assert (entity.index != 0xFF );
129+ if (entity.index == 0xFF ) {
130+ NSError *error = [NSError errorWithDomain: kMBLErrorDomain
131+ code: kMBLErrorOperationInvalid
132+ userInfo: @{NSLocalizedDescriptionKey : @" Can't deinitialize entity that's not initialized" }];
133+ return [BFTask taskWithError: error];
134+ }
118135 return [self .activateEntity writeByteAsync: entity.index];
119136 }];
120137}
@@ -126,7 +143,12 @@ - (BFTask *)performAsyncDeactivationForEntity:(MBLEntityEvent *)entity
126143 }
127144
128145 return [BFTask taskFromMetaWearWithBlock: ^id _Nonnull{
129- assert (entity.index != 0xFF );
146+ if (entity.index == 0xFF ) {
147+ NSError *error = [NSError errorWithDomain: kMBLErrorDomain
148+ code: kMBLErrorOperationInvalid
149+ userInfo: @{NSLocalizedDescriptionKey : @" Can't deinitialize entity that's not initialized" }];
150+ return [BFTask taskWithError: error];
151+ }
130152 return [self .deactivateEntity writeByteAsync: entity.index];
131153 }];
132154}
@@ -139,7 +161,12 @@ - (BFTask *)startNotificationsForEntity:(MBLEntityEvent *)entity
139161
140162 return [[BFTask taskFromMetaWearWithBlock: ^id _Nonnull{
141163 // Turn on notifications for this filter
142- assert (entity.index != 0xFF );
164+ if (entity.index == 0xFF ) {
165+ NSError *error = [NSError errorWithDomain: kMBLErrorDomain
166+ code: kMBLErrorOperationInvalid
167+ userInfo: @{NSLocalizedDescriptionKey : @" Can't start notifications for entity that's not initialized" }];
168+ return [BFTask taskWithError: error];
169+ }
143170 uint8_t packet[] = { entity.index , 0x1 };
144171 return [self .notificationEnable writeDataAsync: [NSData dataWithBytes: &packet length: 2 ]];
145172 }] continueOnMetaWearWithSuccessBlock: ^id _Nullable (BFTask * _Nonnull task) {
@@ -164,12 +191,17 @@ - (BFTask *)stopNotificationsAsyncForEntity:(MBLEntityEvent *)entity
164191 // Turn off global notifications
165192 return [self .globalNotifications stopNotificationsAsync ];
166193 }
167- assert (self.activeNotifications >= 0 );
194+ NSAssert (self.activeNotifications >= 0 , @" Start/Stop notification calls unbalanced. " );
168195 self.activeNotifications = MAX (self.activeNotifications , 0 );
169196 return nil ;
170197 }] continueOnMetaWearWithSuccessBlock: ^id _Nullable (BFTask * _Nonnull task) {
171198 // Turn off notifications for this filter
172- assert (entity.index != 0xFF );
199+ if (entity.index == 0xFF ) {
200+ NSError *error = [NSError errorWithDomain: kMBLErrorDomain
201+ code: kMBLErrorOperationInvalid
202+ userInfo: @{NSLocalizedDescriptionKey : @" Can't stop notifications for entity that's not initialized" }];
203+ return [BFTask taskWithError: error];
204+ }
173205 uint8_t data[] = { entity.index , 0x0 };
174206 return [self .notificationEnable writeDataAsync: [NSData dataWithBytes: &data length: 2 ]];
175207 }];
0 commit comments