You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to give one suggestion regarding the firstOrCreate function.
while doing firstOrCreate keep one primary key so that you can check if that key already exists then only update the data related to that key instead of creating one more record.
to demonstrate suppose say I have an NSManagedObject called Person which keeps his id, balance, name, phone, email as String. So each time I call the update_person_record gives me an array of id, balance, name, phone, email.
So upon calling this let value = ["id":id, "balance" : balance, "name": name, "phone": phone, "email": email]; _ = Person.firstOrCreate(with: value); AERecord.save();
It will create the same person with same details having a different balance. So please provide a primary key so that it will directly update values which have been updated.
The text was updated successfully, but these errors were encountered:
anirudhamahale
changed the title
NSManagedObject.firstOrCreate
Improvement of NSManagedObject.firstOrCreate
Apr 29, 2017
// get person by id if it already exists, otherwise create newletperson=Person.firstOrCreate(with:["id": id])// update data on either existing or a new person
person.name = name
person.email = email
person.phone = phone
person.balance = balance
I would like to give one suggestion regarding the firstOrCreate function.
while doing firstOrCreate keep one primary key so that you can check if that key already exists then only update the data related to that key instead of creating one more record.
to demonstrate suppose say I have an
NSManagedObject
called Person which keeps his id, balance, name, phone, email as String. So each time I call the update_person_record gives me an array of id, balance, name, phone, email.So upon calling this
let value = ["id":id, "balance" : balance, "name": name, "phone": phone, "email": email]; _ = Person.firstOrCreate(with: value); AERecord.save();
It will create the same person with same details having a different balance. So please provide a primary key so that it will directly update values which have been updated.
The text was updated successfully, but these errors were encountered: