Hi everyone,
I'm running into a bit of weird issue around inserting and removing elemnts from an array.
According to the apple docs I would expect removeFirst and append to not cause any issues and just work how it should but obviously I'm missing something.
Most of the time the new elemnt placed with append is inserted at index 0, sometimes its inserted in the middle of the array, see example output.
Anyone have any ideas as to why this is happening?
var Rosters:[Roster]
public func addNewRandomRoster(){
print("-------------")
while Rosters.count > 6 {
print("Rosters over 6, RC: \(Rosters.count)")
Rosters.removeFirst(1)
print("Removed Roster")
print("RC: \(Rosters.count)")
}
let tmp = Roster(id: UUID(), roster: generateWatchRoster())
Rosters.append(tmp)
let i = Rosters.firstIndex(where: {$0.id == tmp.id})
print("Roster inserted at index: \(i!)")
print("Post Inser RC: \(Rosters.count)")
}
Output:
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 0
Post Inser RC: 7
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 0
Post Inser RC: 7
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 0
Post Inser RC: 7
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 0
Post Inser RC: 7
-------------
Rosters over 6, RC: 7
Removed Roster
RC: 6
Roster inserted at index: 4
Post Inser RC: 7
The class does use swift data, I will try without it shorly to verify the behaviour.