SwiftUI + CloudKit Try to add a record with a pop up text field


I am trying to add a record which contains the order and text of it. I can add a new record within the same file using the function "addItem()", but when I try to call this same function from another file I get the error 

Thread 1: EXCBADINSTRUCTION (code=EXCI386INVOP, subcode=0x0) 

on the line 

newItem.order = (listItems.last?.order ?? 0) + 1.


Code Block
var body: some View {
NavigationView {
VStack {
List {
ForEach(listItems, id: \.self) { item in
NavigationLink(destination: ListItemView(listItem: item)) {
Text("\(item.name) - \(item.order)")
}
}
.onDelete(perform: deleteItem)
.onMove(perform: moveItem)
}
// Button(action: addItem) {
// Text("Add Item")
// }
Button(action: {
self.isShowingAlert.toggle()
}) {
Text("Add Item")
}
}
.navigationBarItems(trailing: EditButton())
}
.addSujectPopUp(isShowing: $isShowingAlert, text: $alertInput, title: "Add Subject")
}
func moveItem(indexSet: IndexSet, destination: Int) {
let source = indexSet.first!
if source < destination {
var startIndex = source + 1
let endIndex = destination - 1
var startOrder = listItems[source].order
while startIndex <= endIndex {
listItems[startIndex].order = startOrder
startOrder = startOrder + 1
startIndex = startIndex + 1
}
listItems[source].order = startOrder
} else if destination < source {
var startIndex = destination
let endIndex = source - 1
var startOrder = listItems[destination].order + 1
let newOrder = listItems[destination].order
while startIndex <= endIndex {
listItems[startIndex].order = startOrder
startOrder = startOrder + 1
startIndex = startIndex + 1
}
listItems[source].order = newOrder
}
saveItems()
}
func deleteItem(indexSet: IndexSet) {
let source = indexSet.first!
let listItem = listItems[source]
managedObjectContext.delete(listItem)
saveItems()
}
func addItem() {
let newItem = ListItem(context: managedObjectContext)
newItem.name = alertInput
newItem.order = (listItems.last?.order ?? 0) + 1
saveItems()
}
func saveItems() {
do {
try managedObjectContext.save()
} catch {
print(error)
}
}

Pop Up Code button:
Code Block
let subject = SubjectController()
Button(action: self.subject.addItem) {
Text("Add")
}