I am engaged on my first iOS app. I’ve a button that when pressed, modifications a state variable, which in flip causes a block of code to execute in a background thread. This code ends in creating a brand new object that then will get saved into persistent reminiscence by inserting into my modelContext through modelContext.insert(myObject)
I’ve observed that there is a noticeable UI lag after I press this button. After a number of testing and debugging, I’ve found that modelContext.insert(myObject) is the bottleneck. If I omit this one line of code and maintain all the pieces else as is, my button works nice and there may be 0 lag. As quickly as I add this line of code again in, I discover important lag
What is going on on and the way can I take away my UI lag whereas nonetheless additionally storing my newly created object into persistent reminiscence?
I attempted transferring modelContext.insert(myObject) to varied elements of my code, however irrespective of the place I place it, the minute this will get executed my UI freezes for 0.5-1s till it finishes. I do not need my UI to freeze
That is my triggering button. “amount” here’s a @Binding variable coming from my mother or father view
Button(motion: {
amount += 1
UIImpactFeedbackGenerator(fashion: .medium).impactOccurred()
}) {
Picture(systemName: "plus.circle.fill")
.font(.largeTitle)
}
.padding(.trailing)
}
.body(maxWidth: .infinity)
.background(Coloration.grey.opacity(0.2))
.cornerRadius(10)
Inside my mother or father view, “amount” is a @State variable. A change on this variable triggers the next code
.onChange(of: amount) { oldValue, newValue in
// if didCardVariantChange is true, reset variable after which do nothing
guard !didCardVariantChange else {
didCardVariantChange = false
return
}
didCardVariantChange = false // reset variable
workItem?.cancel() // Cancel earlier work if any
let cardID = viewSetCardData.card_ids[indexVar]
workItem = DispatchWorkItem {
let isCardInCollection = Assortment.incorporates(the place: { $0.card_id == cardID })
// Add or replace the cardboard in assortment as crucial
DispatchQueue.international(qos: .background).async {
if newValue > 0 && !isCardInCollection {
memory_operations.shared.addNewCardToCollection(cardIDToAdd: cardID,
selectedCategory: selectedCardCategory,
selectedVariant: selectedCardVariant,
amount: newValue,
modelContext: modelContext,
cardsArray: allCards)
}
}
DispatchQueue.important.asyncAfter(deadline: .now() + 0.5, execute: workItem!)
The next is the operate for memory_operations.shared.addNewCardToCollection referenced above
func addNewCardToCollection(cardIDToAdd: String, selectedCategory: String, selectedVariant: String = "Major Variant", amount: Int, pricePaid: Double? = nil, modelContext: ModelContext, cardsArray: [CardData]) {
var allVariantsList: [String?] = []
for card in cardsArray the place card.id == cardIDToAdd {
allVariantsList = card.allVariants
}
let newCard = CardsOwned(card_id: cardIDToAdd, allVariants: allVariantsList)
newCard.addQuantity_v2(amount, selectedCategory: selectedCategory, selectedVariant: selectedVariant, unitPrice: pricePaid)
DispatchQueue.important.async {
modelContext.insert(newCard) //Problem happens at this line. Eradicating this line ends in no UI freezing or lag. Including this line again in, ends in a noticeable lag underneath the identical circumstances
strive? modelContext.save()
}
}