Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement of NSManagedObject.firstOrCreate #45

Closed
anirudhamahale opened this issue Apr 29, 2017 · 1 comment
Closed

Improvement of NSManagedObject.firstOrCreate #45

anirudhamahale opened this issue Apr 29, 2017 · 1 comment

Comments

@anirudhamahale
Copy link

anirudhamahale commented Apr 29, 2017

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.

@anirudhamahale anirudhamahale changed the title NSManagedObject.firstOrCreate Improvement of NSManagedObject.firstOrCreate Apr 29, 2017
@tadija
Copy link
Owner

tadija commented Aug 14, 2017

Hi @anirudhamahale,

I suggest you just do it like this:

// get person by id if it already exists, otherwise create new
let person = 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

Cheers,
// T

@tadija tadija closed this as completed Aug 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants