Why do they behave differently from each other ?

  •  insertItemsWithIdentifiers:afterItemWithIdentifier: and - insertSectionsWithIdentifiers:afterSectionWithIdentifier:, their behavior seems to be inconsistent.

Code Block
-(void)testInsertItemsWithIdentifiersAfterItemWithIdentifier_IfHasExist {
    NSArray *items = @[@"1",@"2",@"3",@"5"];
    NSArray *insertItems = @[@"3",@"4"];
    NSArray *sections = @[@1,@2];
    [self.snapshot appendSectionsWithIdentifiers:sections];
    [self.snapshot appendItemsWithIdentifiers:items];
    [self.snapshot insertItemsWithIdentifiers:insertItems afterItemWithIdentifier:@"5"];
    NSArray *expect = @[@"1",@"2",@"3",@"5",@"4"];
    XCTAssertEqualObjects([self.snapshot itemIdentifiers], expect);
}

It has a failure,because [self.snapshot itemIdentifiers] is 1,2,5,3,4

but on the other hand
Code Block - (void)testInsertSectionsWithIdentifiersAfterSectionWithIdentifier_IfHasExist {
    NSArray *sections = @[@1,@2,@3,@5];
    NSArray *insertSections = @[@3,@4];
    [self.snapshot appendSectionsWithIdentifiers:sections];
    [self.snapshot insertSectionsWithIdentifiers:insertSections afterSectionWithIdentifier:@5];
    NSArray *expect = @[@1,@2,@3,@5,@4];
    XCTAssertEqualObjects([self.snapshot sectionIdentifiers], expect);
}

It is success.


Replies

Good question, but the docs do state that identifiers must be unique. You’re attempting to append some items/sections that are not unique so I would expect the behaviour to be undefined. Perhaps you should be using the move or delete APIs to handle your duplicate items?
Duplicate items/sections is not the point.The same initial list @[@1,@2,@3,@5] and same insertion list @[@3,@4] and same toidentifier @5, why different between insertSectionsWithIdentifiers and insertItemsWithIdentifiers.
It seem to ignore the duplicate section @3,only insert @4 when insertSectionsWithIdentifiers.
but when insertItemsWithIdentifiers, it seem to move the @3,not ignore.

Garbage in, garbage out. I believe that duplicate items/sections *is* the point. Frankly I’m surprised the API calls don’t throw an exception when presented with such invalid input.

Anyway, maybe it’s due to how your identifiers are hashed? You’re using numbers in one case and strings in the other. If you use the same type of identifier, does that affect anything?
Code Block
NSArray *sections = @[@"1",@"2",@"3",@"5"];
    NSArray *insertSections = @[@"3",@"4"];
    [self.stringSnapshot appendSectionsWithIdentifiers:sections];
    [self.stringSnapshot insertSectionsWithIdentifiers:insertSections afterSectionWithIdentifier:@"5"];
    NSArray *expect = @[@"1",@"2",@"3",@"5",@"4"];
    XCTAssertEqualObjects([self.stringSnapshot sectionIdentifiers], expect);

Code Block
    NSArray *items = @[@"1",@"2",@"3",@"5"];
    NSArray *insertItems = @[@"3",@"4"];
    NSArray *sections = @[@1,@2];
    [self.stringSnapshot appendSectionsWithIdentifiers:sections];
    [self.stringSnapshot appendItemsWithIdentifiers:items];
    [self.stringSnapshot insertItemsWithIdentifiers:insertItems afterItemWithIdentifier:@"5"];
    NSArray *expect = @[@"1",@"2",@"3",@"5",@"4"];
    XCTAssertEqualObjects([self.stringSnapshot itemIdentifiers], expect);

I tried using strings in both cases, but the result is same as above.The same goes for Numbers.