@@ -152,11 +152,9 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
152
152
NSNumber *newAmount = [PFInternalUtils addNumber: self .amount
153
153
withNumber: ((PFIncrementOperation *)previous).amount];
154
154
return [PFIncrementOperation incrementWithAmount: newAmount];
155
- } else {
156
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
157
- reason: @" Operation is invalid after previous operation."
158
- userInfo: nil ];
159
155
}
156
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
157
+ return nil ;
160
158
}
161
159
162
160
- (id )applyToValue : (id )oldValue forKey : (NSString *)key {
@@ -210,31 +208,26 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
210
208
NSArray *newArray = [oldArray arrayByAddingObjectsFromArray: self .objects];
211
209
return [PFSetOperation setWithValue: newArray];
212
210
} else {
213
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
214
- reason: @" You can't add an item to a non-array."
215
- userInfo: nil ];
211
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
212
+ return nil ;
216
213
}
217
214
} else if ([previous isKindOfClass: [PFAddOperation class ]]) {
218
215
NSMutableArray *newObjects = [((PFAddOperation *)previous).objects mutableCopy ];
219
216
[newObjects addObjectsFromArray: self .objects];
220
217
return [[self class ] addWithObjects: newObjects];
221
- } else {
222
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
223
- reason: @" Operation is invalid after previous operation."
224
- userInfo: nil ];
225
218
}
219
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
220
+ return nil ;
226
221
}
227
222
228
223
- (id )applyToValue : (id )oldValue forKey : (NSString *)key {
229
224
if (!oldValue) {
230
225
return [self .objects mutableCopy ];
231
226
} else if ([oldValue isKindOfClass: [NSArray class ]]) {
232
227
return [((NSArray *)oldValue)arrayByAddingObjectsFromArray:self .objects];
233
- } else {
234
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
235
- reason: @" Operation is invalid after previous operation."
236
- userInfo: nil ];
237
228
}
229
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
230
+ return nil ;
238
231
}
239
232
240
233
@end
@@ -274,18 +267,15 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
274
267
NSArray *oldArray = (((PFSetOperation *)previous).value );
275
268
return [PFSetOperation setWithValue: [self applyToValue: oldArray forKey: nil ]];
276
269
} else {
277
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
278
- reason: @" You can't add an item to a non-array."
279
- userInfo: nil ];
270
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
271
+ return nil ;
280
272
}
281
273
} else if ([previous isKindOfClass: [PFAddUniqueOperation class ]]) {
282
274
NSArray *previousObjects = ((PFAddUniqueOperation *)previous).objects ;
283
275
return [[self class ] addUniqueWithObjects: [self applyToValue: previousObjects forKey: nil ]];
284
- } else {
285
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
286
- reason: @" Operation is invalid after previous operation."
287
- userInfo: nil ];
288
276
}
277
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
278
+ return nil ;
289
279
}
290
280
291
281
- (id )applyToValue : (id )oldValue forKey : (NSString *)key {
@@ -311,11 +301,9 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key {
311
301
}
312
302
}
313
303
return newValue;
314
- } else {
315
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
316
- reason: @" Operation is invalid after previous operation."
317
- userInfo: nil ];
318
304
}
305
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
306
+ return nil ;
319
307
}
320
308
321
309
@end
@@ -348,26 +336,23 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
348
336
if (!previous) {
349
337
return self;
350
338
} else if ([previous isKindOfClass: [PFDeleteOperation class ]]) {
351
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
352
- reason: @" You can't remove items from a deleted array."
353
- userInfo: nil ];
339
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
340
+ return nil ;
354
341
} else if ([previous isKindOfClass: [PFSetOperation class ]]) {
355
342
if ([((PFSetOperation *)previous).value isKindOfClass: [NSArray class ]]) {
356
343
NSArray *oldArray = ((PFSetOperation *)previous).value ;
357
344
return [PFSetOperation setWithValue: [self applyToValue: oldArray forKey: nil ]];
358
345
} else {
359
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
360
- reason: @" You can't add an item to a non-array."
361
- userInfo: nil ];
346
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
347
+ return nil ;
362
348
}
363
349
} else if ([previous isKindOfClass: [PFRemoveOperation class ]]) {
364
350
NSArray *newObjects = [((PFRemoveOperation *)previous).objects arrayByAddingObjectsFromArray: self .objects];
365
351
return [PFRemoveOperation removeWithObjects: newObjects];
366
- } else {
367
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
368
- reason: @" Operation is invalid after previous operation."
369
- userInfo: nil ];
370
352
}
353
+
354
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
355
+ return nil ;
371
356
}
372
357
373
358
- (id )applyToValue : (id )oldValue forKey : (NSString *)key {
@@ -393,11 +378,9 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key {
393
378
}
394
379
}
395
380
return newValue;
396
- } else {
397
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
398
- reason: @" Operation is invalid after previous operation."
399
- userInfo: nil ];
400
381
}
382
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
383
+ return nil ;
401
384
}
402
385
403
386
@end
@@ -426,10 +409,8 @@ + (instancetype)addRelationToObjects:(NSArray *)targets {
426
409
}
427
410
428
411
for (PFObject *target in targets) {
429
- if (![[target parseClassName ] isEqualToString: op.targetClass]) {
430
- [NSException raise :NSInvalidArgumentException
431
- format: @" All objects in a relation must be of the same class." ];
432
- }
412
+ PFParameterAssert ([target.parseClassName isEqualToString: op.targetClass],
413
+ @" All objects in a relation must be of the same class." );
433
414
[op.relationsToAdd addObject: target];
434
415
}
435
416
@@ -443,10 +424,8 @@ + (instancetype)removeRelationToObjects:(NSArray *)targets {
443
424
}
444
425
445
426
for (PFObject *target in targets) {
446
- if (![[target parseClassName ] isEqualToString: operation.targetClass]) {
447
- [NSException raise :NSInvalidArgumentException
448
- format: @" All objects in a relation must be of the same class." ];
449
- }
427
+ PFParameterAssert ([target.parseClassName isEqualToString: operation.targetClass],
428
+ @" All objects in a relation must be of the same class." );
450
429
[operation.relationsToRemove addObject: target];
451
430
}
452
431
@@ -496,57 +475,49 @@ - (id)encodeWithObjectEncoder:(PFEncoder *)objectEncoder {
496
475
return removeDict;
497
476
}
498
477
499
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
500
- reason: @" A PFRelationOperation was created without any data."
501
- userInfo: nil ];
478
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
479
+ return nil ;
502
480
}
503
481
504
482
- (PFFieldOperation *)mergeWithPrevious : (PFFieldOperation *)previous {
505
483
if (!previous) {
506
484
return self;
507
- } else if ([previous isKindOfClass: [PFDeleteOperation class ]]) {
508
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
509
- reason: @" You can't modify a relation after deleting it."
510
- userInfo: nil ];
511
- } else if ([previous isKindOfClass: [PFRelationOperation class ]]) {
512
- PFRelationOperation *previousOperation = (PFRelationOperation *)previous;
513
- if (previousOperation.targetClass &&
514
- ![previousOperation.targetClass isEqualToString: self .targetClass]) {
515
- [NSException raise :NSInvalidArgumentException
516
- format: @" Related object object must be of class %@ , but %@ was passed in" ,
517
- previousOperation.targetClass,
518
- self .targetClass];
519
- } else {
520
- // TODO: (nlutsenko) This logic seems to be messed up. We should return a new operation here, also merging logic seems funky.
521
- NSSet *newRelationsToAdd = [self .relationsToAdd copy ];
522
- NSSet *newRelationsToRemove = [self .relationsToRemove copy ];
523
- [self .relationsToAdd removeAllObjects ];
524
- [self .relationsToRemove removeAllObjects ];
525
-
526
- for (NSString *objectId in previousOperation.relationsToAdd ) {
527
- [self .relationsToRemove removeObject: objectId];
528
- [self .relationsToAdd addObject: objectId];
529
- }
530
- for (NSString *objectId in previousOperation.relationsToRemove ) {
531
- [self .relationsToRemove removeObject: objectId];
532
- [self .relationsToRemove addObject: objectId];
533
- }
485
+ }
534
486
535
- for (NSString *objectId in newRelationsToAdd) {
536
- [self .relationsToRemove removeObject: objectId];
537
- [self .relationsToAdd addObject: objectId];
538
- }
539
- for (NSString *objectId in newRelationsToRemove) {
540
- [self .relationsToRemove removeObject: objectId];
541
- [self .relationsToRemove addObject: objectId];
542
- }
543
- }
544
- return self;
545
- } else {
546
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
547
- reason: @" Operation is invalid after previous operation."
548
- userInfo: nil ];
487
+ PFConsistencyAssert (![previous isKindOfClass: [PFDeleteOperation class ]], @" You can't modify a relation after deleting it" );
488
+ PFConsistencyAssert ([previous isKindOfClass: [PFRelationOperation class ]], @" Operation is invalid after previous operation" );
489
+
490
+ PFRelationOperation *previousOperation = (PFRelationOperation *)previous;
491
+
492
+ PFParameterAssert (!previousOperation.targetClass || [previousOperation.targetClass isEqualToString: self .targetClass],
493
+ @" Related object object must be of class %@ , but %@ was passed in" ,
494
+ previousOperation.targetClass ,
495
+ self.targetClass );
496
+
497
+ // TODO: (nlutsenko) This logic seems to be messed up. We should return a new operation here, also merging logic seems funky.
498
+ NSSet *newRelationsToAdd = [self .relationsToAdd copy ];
499
+ NSSet *newRelationsToRemove = [self .relationsToRemove copy ];
500
+ [self .relationsToAdd removeAllObjects ];
501
+ [self .relationsToRemove removeAllObjects ];
502
+
503
+ for (NSString *objectId in previousOperation.relationsToAdd ) {
504
+ [self .relationsToRemove removeObject: objectId];
505
+ [self .relationsToAdd addObject: objectId];
549
506
}
507
+ for (NSString *objectId in previousOperation.relationsToRemove ) {
508
+ [self .relationsToRemove removeObject: objectId];
509
+ [self .relationsToRemove addObject: objectId];
510
+ }
511
+
512
+ for (NSString *objectId in newRelationsToAdd) {
513
+ [self .relationsToRemove removeObject: objectId];
514
+ [self .relationsToAdd addObject: objectId];
515
+ }
516
+ for (NSString *objectId in newRelationsToRemove) {
517
+ [self .relationsToRemove removeObject: objectId];
518
+ [self .relationsToRemove addObject: objectId];
519
+ }
520
+ return self;
550
521
}
551
522
552
523
- (id )applyToValue : (id )oldValue forKey : (NSString *)key {
@@ -557,20 +528,16 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key {
557
528
relation = oldValue;
558
529
if (self.targetClass ) {
559
530
if (relation .targetClass ) {
560
- if (![relation .targetClass isEqualToString: targetClass]) {
561
- [NSException raise :NSInvalidArgumentException
562
- format: @" Related object object must be of class %@ , but %@ was passed in" ,
563
- relation .targetClass,
564
- self .targetClass];
565
- }
531
+ PFParameterAssert ([relation .targetClass isEqualToString: targetClass],
532
+ @" Related object object must be of class %@ , but %@ was passed in" ,
533
+ relation .targetClass , self.targetClass );
566
534
} else {
567
535
relation .targetClass = self.targetClass ;
568
536
}
569
537
}
570
538
} else {
571
- @throw [NSException exceptionWithName: NSInternalInconsistencyException
572
- reason: @" Operation is invalid after previous operation."
573
- userInfo: nil ];
539
+ [NSException raise :NSInternalInconsistencyException format: @" Operation is invalid after previous operation." ];
540
+ return nil ;
574
541
}
575
542
576
543
for (PFObject *object in self.relationsToAdd ) {
0 commit comments